2010-04-22 22:06:42 +02:00
|
|
|
//===-- FastISel.cpp - Implementation of the FastISel class ---------------===//
|
2008-08-13 22:19:35 +02:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file contains the implementation of the FastISel class.
|
|
|
|
//
|
2008-09-30 22:48:29 +02:00
|
|
|
// "Fast" instruction selection is designed to emit very poor code quickly.
|
|
|
|
// Also, it is not designed to be able to do much lowering, so most illegal
|
2008-10-13 03:59:13 +02:00
|
|
|
// types (e.g. i64 on 32-bit targets) and operations are not supported. It is
|
|
|
|
// also not intended to be able to do much optimization, except in a few cases
|
|
|
|
// where doing optimizations reduces overall compile time. For example, folding
|
|
|
|
// constants into immediate fields is often done, because it's cheap and it
|
|
|
|
// reduces the number of instructions later phases have to examine.
|
2008-09-30 22:48:29 +02:00
|
|
|
//
|
|
|
|
// "Fast" instruction selection is able to fail gracefully and transfer
|
|
|
|
// control to the SelectionDAG selector for operations that it doesn't
|
2008-10-13 03:59:13 +02:00
|
|
|
// support. In many cases, this allows us to avoid duplicating a lot of
|
2008-09-30 22:48:29 +02:00
|
|
|
// the complicated lowering logic that SelectionDAG currently has.
|
|
|
|
//
|
|
|
|
// The intended use for "fast" instruction selection is "-O0" mode
|
|
|
|
// compilation, where the quality of the generated code is irrelevant when
|
2008-10-13 03:59:13 +02:00
|
|
|
// weighed against the speed at which the code can be generated. Also,
|
2008-09-30 22:48:29 +02:00
|
|
|
// at -O0, the LLVM optimizers are not running, and this makes the
|
|
|
|
// compile time of codegen a much higher portion of the overall compile
|
2008-10-13 03:59:13 +02:00
|
|
|
// time. Despite its limitations, "fast" instruction selection is able to
|
2008-09-30 22:48:29 +02:00
|
|
|
// handle enough code on its own to provide noticeable overall speedups
|
|
|
|
// in -O0 compiles.
|
|
|
|
//
|
|
|
|
// Basic operations are supported in a target-independent way, by reading
|
|
|
|
// the same instruction descriptions that the SelectionDAG selector reads,
|
|
|
|
// and identifying simple arithmetic operations that can be directly selected
|
2008-10-13 03:59:13 +02:00
|
|
|
// from simple operators. More complicated operations currently require
|
2008-09-30 22:48:29 +02:00
|
|
|
// target-specific code.
|
|
|
|
//
|
2008-08-13 22:19:35 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2011-11-16 22:05:28 +01:00
|
|
|
#define DEBUG_TYPE "isel"
|
2008-08-13 22:19:35 +02:00
|
|
|
#include "llvm/CodeGen/FastISel.h"
|
2013-06-16 22:34:15 +02:00
|
|
|
#include "llvm/ADT/Optional.h"
|
2012-12-03 17:50:05 +01:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
|
|
|
#include "llvm/Analysis/Loads.h"
|
|
|
|
#include "llvm/CodeGen/Analysis.h"
|
2010-07-07 18:01:37 +02:00
|
|
|
#include "llvm/CodeGen/FunctionLoweringInfo.h"
|
2008-08-13 22:19:35 +02:00
|
|
|
#include "llvm/CodeGen/MachineInstrBuilder.h"
|
2008-09-25 19:05:24 +02:00
|
|
|
#include "llvm/CodeGen/MachineModuleInfo.h"
|
2008-08-13 22:19:35 +02:00
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
2013-01-02 12:36:10 +01:00
|
|
|
#include "llvm/IR/DataLayout.h"
|
2014-03-06 01:46:21 +01:00
|
|
|
#include "llvm/IR/DebugInfo.h"
|
2013-01-02 12:36:10 +01:00
|
|
|
#include "llvm/IR/Function.h"
|
|
|
|
#include "llvm/IR/GlobalVariable.h"
|
|
|
|
#include "llvm/IR/Instructions.h"
|
|
|
|
#include "llvm/IR/IntrinsicInst.h"
|
|
|
|
#include "llvm/IR/Operator.h"
|
2012-12-03 17:50:05 +01:00
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2008-08-13 22:19:35 +02:00
|
|
|
#include "llvm/Target/TargetInstrInfo.h"
|
2012-08-03 06:06:28 +02:00
|
|
|
#include "llvm/Target/TargetLibraryInfo.h"
|
2008-08-21 00:45:34 +02:00
|
|
|
#include "llvm/Target/TargetLowering.h"
|
2008-08-20 23:05:57 +02:00
|
|
|
#include "llvm/Target/TargetMachine.h"
|
2008-08-13 22:19:35 +02:00
|
|
|
using namespace llvm;
|
|
|
|
|
2011-11-28 20:59:09 +01:00
|
|
|
STATISTIC(NumFastIselSuccessIndependent, "Number of insts selected by "
|
|
|
|
"target-independent selector");
|
|
|
|
STATISTIC(NumFastIselSuccessTarget, "Number of insts selected by "
|
|
|
|
"target-specific selector");
|
2011-11-29 20:40:47 +01:00
|
|
|
STATISTIC(NumFastIselDead, "Number of dead insts removed on failure");
|
2011-11-16 22:05:28 +01:00
|
|
|
|
2010-07-10 11:00:22 +02:00
|
|
|
/// startNewBlock - Set the current block to which generated machine
|
|
|
|
/// instructions will be appended, and clear the local CSE map.
|
|
|
|
///
|
|
|
|
void FastISel::startNewBlock() {
|
|
|
|
LocalValueMap.clear();
|
|
|
|
|
2013-07-04 06:53:49 +02:00
|
|
|
// Instructions are appended to FuncInfo.MBB. If the basic block already
|
2013-07-04 06:32:39 +02:00
|
|
|
// contains labels or copies, use the last instruction as the last local
|
|
|
|
// value.
|
2014-04-14 02:51:57 +02:00
|
|
|
EmitStartPt = nullptr;
|
2013-07-04 06:32:39 +02:00
|
|
|
if (!FuncInfo.MBB->empty())
|
|
|
|
EmitStartPt = &FuncInfo.MBB->back();
|
2011-08-19 00:06:10 +02:00
|
|
|
LastLocalValue = EmitStartPt;
|
|
|
|
}
|
|
|
|
|
2013-02-11 02:27:15 +01:00
|
|
|
bool FastISel::LowerArguments() {
|
|
|
|
if (!FuncInfo.CanLowerReturn)
|
|
|
|
// Fallback to SDISel argument lowering code to deal with sret pointer
|
|
|
|
// parameter.
|
|
|
|
return false;
|
2013-07-08 02:37:03 +02:00
|
|
|
|
2013-02-11 02:27:15 +01:00
|
|
|
if (!FastLowerArguments())
|
|
|
|
return false;
|
|
|
|
|
2013-06-22 00:56:30 +02:00
|
|
|
// Enter arguments into ValueMap for uses in non-entry BBs.
|
2013-02-11 02:27:15 +01:00
|
|
|
for (Function::const_arg_iterator I = FuncInfo.Fn->arg_begin(),
|
|
|
|
E = FuncInfo.Fn->arg_end(); I != E; ++I) {
|
2013-06-22 00:56:30 +02:00
|
|
|
DenseMap<const Value *, unsigned>::iterator VI = LocalValueMap.find(I);
|
|
|
|
assert(VI != LocalValueMap.end() && "Missed an argument?");
|
|
|
|
FuncInfo.ValueMap[I] = VI->second;
|
2013-02-11 02:27:15 +01:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-08-19 00:06:10 +02:00
|
|
|
void FastISel::flushLocalValueMap() {
|
|
|
|
LocalValueMap.clear();
|
|
|
|
LastLocalValue = EmitStartPt;
|
|
|
|
recomputeInsertPt();
|
2010-07-10 11:00:22 +02:00
|
|
|
}
|
|
|
|
|
2010-05-12 01:54:07 +02:00
|
|
|
bool FastISel::hasTrivialKill(const Value *V) const {
|
2010-05-15 00:53:18 +02:00
|
|
|
// Don't consider constants or arguments to have trivial kills.
|
2010-05-12 01:54:07 +02:00
|
|
|
const Instruction *I = dyn_cast<Instruction>(V);
|
2010-05-15 00:53:18 +02:00
|
|
|
if (!I)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// No-op casts are trivially coalesced by fast-isel.
|
|
|
|
if (const CastInst *Cast = dyn_cast<CastInst>(I))
|
2014-02-18 23:05:46 +01:00
|
|
|
if (Cast->isNoopCast(DL.getIntPtrType(Cast->getContext())) &&
|
2012-11-01 09:07:29 +01:00
|
|
|
!hasTrivialKill(Cast->getOperand(0)))
|
2010-05-15 00:53:18 +02:00
|
|
|
return false;
|
|
|
|
|
2011-11-16 00:34:05 +01:00
|
|
|
// GEPs with all zero indices are trivially coalesced by fast-isel.
|
|
|
|
if (const GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(I))
|
|
|
|
if (GEP->hasAllZeroIndices() && !hasTrivialKill(GEP->getOperand(0)))
|
|
|
|
return false;
|
|
|
|
|
2010-05-15 00:53:18 +02:00
|
|
|
// Only instructions with a single use in the same basic block are considered
|
|
|
|
// to have trivial kills.
|
|
|
|
return I->hasOneUse() &&
|
|
|
|
!(I->getOpcode() == Instruction::BitCast ||
|
|
|
|
I->getOpcode() == Instruction::PtrToInt ||
|
|
|
|
I->getOpcode() == Instruction::IntToPtr) &&
|
2014-03-09 04:16:01 +01:00
|
|
|
cast<Instruction>(*I->user_begin())->getParent() == I->getParent();
|
2010-05-12 01:54:07 +02:00
|
|
|
}
|
|
|
|
|
2010-04-15 03:51:59 +02:00
|
|
|
unsigned FastISel::getRegForValue(const Value *V) {
|
2009-08-11 00:56:29 +02:00
|
|
|
EVT RealVT = TLI.getValueType(V->getType(), /*AllowUnknown=*/true);
|
2009-04-07 22:40:11 +02:00
|
|
|
// Don't handle non-simple values in FastISel.
|
|
|
|
if (!RealVT.isSimple())
|
|
|
|
return 0;
|
2008-09-10 23:01:08 +02:00
|
|
|
|
2008-12-08 08:57:47 +01:00
|
|
|
// Ignore illegal types. We must do this before looking up the value
|
|
|
|
// in ValueMap because Arguments are given virtual registers regardless
|
|
|
|
// of whether FastISel can handle them.
|
2009-08-11 22:47:22 +02:00
|
|
|
MVT VT = RealVT.getSimpleVT();
|
2008-09-10 23:01:08 +02:00
|
|
|
if (!TLI.isTypeLegal(VT)) {
|
2011-05-26 01:49:02 +02:00
|
|
|
// Handle integer promotions, though, because they're common and easy.
|
|
|
|
if (VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16)
|
2009-08-12 02:36:31 +02:00
|
|
|
VT = TLI.getTypeToTransformTo(V->getContext(), VT).getSimpleVT();
|
2008-09-10 23:01:08 +02:00
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-03-20 02:07:47 +01:00
|
|
|
// Look up the value to see if we already have a register for it.
|
|
|
|
unsigned Reg = lookUpRegForValue(V);
|
2008-12-08 08:57:47 +01:00
|
|
|
if (Reg != 0)
|
|
|
|
return Reg;
|
|
|
|
|
2010-05-06 02:02:14 +02:00
|
|
|
// In bottom-up mode, just create the virtual register which will be used
|
|
|
|
// to hold the value. It will be materialized later.
|
2010-07-10 11:00:22 +02:00
|
|
|
if (isa<Instruction>(V) &&
|
|
|
|
(!isa<AllocaInst>(V) ||
|
|
|
|
!FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(V))))
|
|
|
|
return FuncInfo.InitializeRegForValue(V);
|
|
|
|
|
2012-10-03 10:10:01 +02:00
|
|
|
SavePoint SaveInsertPt = enterLocalValueArea();
|
2010-07-10 11:00:22 +02:00
|
|
|
|
|
|
|
// Materialize the value in a register. Emit any instructions in the
|
|
|
|
// local value area.
|
|
|
|
Reg = materializeRegForValue(V, VT);
|
|
|
|
|
2012-10-03 10:10:01 +02:00
|
|
|
leaveLocalValueArea(SaveInsertPt);
|
2010-05-06 02:02:14 +02:00
|
|
|
|
2010-07-10 11:00:22 +02:00
|
|
|
return Reg;
|
2010-05-04 01:36:34 +02:00
|
|
|
}
|
|
|
|
|
2010-08-17 03:30:33 +02:00
|
|
|
/// materializeRegForValue - Helper for getRegForValue. This function is
|
2010-05-04 01:36:34 +02:00
|
|
|
/// called when the value isn't already available in a register and must
|
|
|
|
/// be materialized with new instructions.
|
|
|
|
unsigned FastISel::materializeRegForValue(const Value *V, MVT VT) {
|
|
|
|
unsigned Reg = 0;
|
|
|
|
|
2010-04-15 03:51:59 +02:00
|
|
|
if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
|
2008-09-20 00:16:54 +02:00
|
|
|
if (CI->getValue().getActiveBits() <= 64)
|
|
|
|
Reg = FastEmit_i(VT, VT, ISD::Constant, CI->getZExtValue());
|
2008-09-10 22:11:02 +02:00
|
|
|
} else if (isa<AllocaInst>(V)) {
|
2008-09-20 00:16:54 +02:00
|
|
|
Reg = TargetMaterializeAlloca(cast<AllocaInst>(V));
|
2008-08-28 23:19:07 +02:00
|
|
|
} else if (isa<ConstantPointerNull>(V)) {
|
2008-10-08 00:03:27 +02:00
|
|
|
// Translate this as an integer zero so that it can be
|
|
|
|
// local-CSE'd with actual integer zeros.
|
2009-08-13 23:58:54 +02:00
|
|
|
Reg =
|
2014-02-18 23:05:46 +01:00
|
|
|
getRegForValue(Constant::getNullValue(DL.getIntPtrType(V->getContext())));
|
2010-04-15 03:51:59 +02:00
|
|
|
} else if (const ConstantFP *CF = dyn_cast<ConstantFP>(V)) {
|
2011-04-28 02:42:03 +02:00
|
|
|
if (CF->isNullValue()) {
|
2011-04-28 00:41:55 +02:00
|
|
|
Reg = TargetMaterializeFloatZero(CF);
|
|
|
|
} else {
|
|
|
|
// Try to emit the constant directly.
|
|
|
|
Reg = FastEmit_f(VT, VT, ISD::ConstantFP, CF);
|
|
|
|
}
|
2008-08-27 20:10:19 +02:00
|
|
|
|
|
|
|
if (!Reg) {
|
2010-04-13 19:07:06 +02:00
|
|
|
// Try to emit the constant by using an integer constant with a cast.
|
2008-08-27 20:10:19 +02:00
|
|
|
const APFloat &Flt = CF->getValueAPF();
|
2009-08-11 00:56:29 +02:00
|
|
|
EVT IntVT = TLI.getPointerTy();
|
2008-08-27 20:10:19 +02:00
|
|
|
|
|
|
|
uint64_t x[2];
|
|
|
|
uint32_t IntBitWidth = IntVT.getSizeInBits();
|
2008-10-10 01:00:39 +02:00
|
|
|
bool isExact;
|
|
|
|
(void) Flt.convertToInteger(x, IntBitWidth, /*isSigned=*/true,
|
2012-03-20 02:07:56 +01:00
|
|
|
APFloat::rmTowardZero, &isExact);
|
2008-10-10 01:00:39 +02:00
|
|
|
if (isExact) {
|
2011-07-18 23:45:40 +02:00
|
|
|
APInt IntVal(IntBitWidth, x);
|
2008-08-27 20:10:19 +02:00
|
|
|
|
2009-07-22 02:24:57 +02:00
|
|
|
unsigned IntegerReg =
|
2009-07-25 01:12:02 +02:00
|
|
|
getRegForValue(ConstantInt::get(V->getContext(), IntVal));
|
2008-09-20 00:16:54 +02:00
|
|
|
if (IntegerReg != 0)
|
2010-05-12 01:54:07 +02:00
|
|
|
Reg = FastEmit_r(IntVT.getSimpleVT(), VT, ISD::SINT_TO_FP,
|
|
|
|
IntegerReg, /*Kill=*/false);
|
2008-09-20 00:16:54 +02:00
|
|
|
}
|
2008-08-27 20:10:19 +02:00
|
|
|
}
|
2010-04-15 03:51:59 +02:00
|
|
|
} else if (const Operator *Op = dyn_cast<Operator>(V)) {
|
2010-07-01 04:58:57 +02:00
|
|
|
if (!SelectOperator(Op, Op->getOpcode()))
|
|
|
|
if (!isa<Instruction>(Op) ||
|
|
|
|
!TargetSelectInstruction(cast<Instruction>(Op)))
|
|
|
|
return 0;
|
2010-06-21 16:17:46 +02:00
|
|
|
Reg = lookUpRegForValue(Op);
|
2008-08-28 23:19:07 +02:00
|
|
|
} else if (isa<UndefValue>(V)) {
|
2008-09-04 01:32:19 +02:00
|
|
|
Reg = createResultReg(TLI.getRegClassFor(VT));
|
2014-02-18 23:05:46 +01:00
|
|
|
BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
|
2010-07-10 11:00:22 +02:00
|
|
|
TII.get(TargetOpcode::IMPLICIT_DEF), Reg);
|
2008-08-27 20:10:19 +02:00
|
|
|
}
|
2010-11-23 04:31:01 +01:00
|
|
|
|
2008-09-25 03:28:51 +02:00
|
|
|
// If target-independent code couldn't handle the value, give target-specific
|
|
|
|
// code a try.
|
2008-09-06 01:36:01 +02:00
|
|
|
if (!Reg && isa<Constant>(V))
|
2008-09-20 00:16:54 +02:00
|
|
|
Reg = TargetMaterializeConstant(cast<Constant>(V));
|
2010-11-23 04:31:01 +01:00
|
|
|
|
2008-09-20 00:16:54 +02:00
|
|
|
// Don't cache constant materializations in the general ValueMap.
|
|
|
|
// To do so would require tracking what uses they dominate.
|
2010-07-10 11:00:22 +02:00
|
|
|
if (Reg != 0) {
|
2008-09-25 03:28:51 +02:00
|
|
|
LocalValueMap[V] = Reg;
|
2010-07-10 11:00:22 +02:00
|
|
|
LastLocalValue = MRI.getVRegDef(Reg);
|
|
|
|
}
|
2008-09-04 01:32:19 +02:00
|
|
|
return Reg;
|
2008-08-27 20:10:19 +02:00
|
|
|
}
|
|
|
|
|
2010-04-15 03:51:59 +02:00
|
|
|
unsigned FastISel::lookUpRegForValue(const Value *V) {
|
2008-09-09 03:26:59 +02:00
|
|
|
// Look up the value to see if we already have a register for it. We
|
|
|
|
// cache values defined by Instructions across blocks, and other values
|
|
|
|
// only locally. This is because Instructions already have the SSA
|
2010-05-04 01:36:34 +02:00
|
|
|
// def-dominates-use requirement enforced.
|
2010-07-07 18:29:44 +02:00
|
|
|
DenseMap<const Value *, unsigned>::iterator I = FuncInfo.ValueMap.find(V);
|
|
|
|
if (I != FuncInfo.ValueMap.end())
|
2010-06-21 16:21:47 +02:00
|
|
|
return I->second;
|
2012-10-03 10:10:01 +02:00
|
|
|
return LocalValueMap[V];
|
2008-09-09 03:26:59 +02:00
|
|
|
}
|
|
|
|
|
2008-08-30 02:38:46 +02:00
|
|
|
/// UpdateValueMap - Update the value map to include the new mapping for this
|
|
|
|
/// instruction, or insert an extra copy to get the result in a previous
|
|
|
|
/// determined register.
|
|
|
|
/// NOTE: This is only necessary because we might select a block that uses
|
|
|
|
/// a value before we select the block that defines the value. It might be
|
|
|
|
/// possible to fix this by selecting blocks in reverse postorder.
|
2011-05-16 23:06:17 +02:00
|
|
|
void FastISel::UpdateValueMap(const Value *I, unsigned Reg, unsigned NumRegs) {
|
2008-09-05 20:18:20 +02:00
|
|
|
if (!isa<Instruction>(I)) {
|
|
|
|
LocalValueMap[I] = Reg;
|
2011-05-16 23:06:17 +02:00
|
|
|
return;
|
2009-04-12 09:45:01 +02:00
|
|
|
}
|
2010-11-23 04:31:01 +01:00
|
|
|
|
2010-07-07 18:29:44 +02:00
|
|
|
unsigned &AssignedReg = FuncInfo.ValueMap[I];
|
2009-04-12 09:45:01 +02:00
|
|
|
if (AssignedReg == 0)
|
2010-07-10 11:00:22 +02:00
|
|
|
// Use the new register.
|
2009-04-12 09:45:01 +02:00
|
|
|
AssignedReg = Reg;
|
2009-04-12 09:46:30 +02:00
|
|
|
else if (Reg != AssignedReg) {
|
2010-07-10 11:00:22 +02:00
|
|
|
// Arrange for uses of AssignedReg to be replaced by uses of Reg.
|
2011-05-16 23:06:17 +02:00
|
|
|
for (unsigned i = 0; i < NumRegs; i++)
|
|
|
|
FuncInfo.RegFixups[AssignedReg+i] = Reg+i;
|
2010-07-10 11:00:22 +02:00
|
|
|
|
|
|
|
AssignedReg = Reg;
|
2008-09-05 20:18:20 +02:00
|
|
|
}
|
2008-08-30 02:38:46 +02:00
|
|
|
}
|
|
|
|
|
2010-05-12 01:54:07 +02:00
|
|
|
std::pair<unsigned, bool> FastISel::getRegForGEPIndex(const Value *Idx) {
|
2008-12-08 08:57:47 +01:00
|
|
|
unsigned IdxN = getRegForValue(Idx);
|
|
|
|
if (IdxN == 0)
|
|
|
|
// Unhandled operand. Halt "fast" selection and bail.
|
2010-05-12 01:54:07 +02:00
|
|
|
return std::pair<unsigned, bool>(0, false);
|
|
|
|
|
|
|
|
bool IdxNIsKill = hasTrivialKill(Idx);
|
2008-12-08 08:57:47 +01:00
|
|
|
|
|
|
|
// If the index is smaller or larger than intptr_t, truncate or extend it.
|
2009-08-11 23:59:30 +02:00
|
|
|
MVT PtrVT = TLI.getPointerTy();
|
2009-08-11 00:56:29 +02:00
|
|
|
EVT IdxVT = EVT::getEVT(Idx->getType(), /*HandleUnknown=*/false);
|
2010-05-12 01:54:07 +02:00
|
|
|
if (IdxVT.bitsLT(PtrVT)) {
|
|
|
|
IdxN = FastEmit_r(IdxVT.getSimpleVT(), PtrVT, ISD::SIGN_EXTEND,
|
|
|
|
IdxN, IdxNIsKill);
|
|
|
|
IdxNIsKill = true;
|
|
|
|
}
|
|
|
|
else if (IdxVT.bitsGT(PtrVT)) {
|
|
|
|
IdxN = FastEmit_r(IdxVT.getSimpleVT(), PtrVT, ISD::TRUNCATE,
|
|
|
|
IdxN, IdxNIsKill);
|
|
|
|
IdxNIsKill = true;
|
|
|
|
}
|
|
|
|
return std::pair<unsigned, bool>(IdxN, IdxNIsKill);
|
2008-12-08 08:57:47 +01:00
|
|
|
}
|
|
|
|
|
2010-07-10 11:00:22 +02:00
|
|
|
void FastISel::recomputeInsertPt() {
|
|
|
|
if (getLastLocalValue()) {
|
|
|
|
FuncInfo.InsertPt = getLastLocalValue();
|
2010-07-20 00:48:56 +02:00
|
|
|
FuncInfo.MBB = FuncInfo.InsertPt->getParent();
|
2010-07-10 11:00:22 +02:00
|
|
|
++FuncInfo.InsertPt;
|
|
|
|
} else
|
|
|
|
FuncInfo.InsertPt = FuncInfo.MBB->getFirstNonPHI();
|
|
|
|
|
|
|
|
// Now skip past any EH_LABELs, which must remain at the beginning.
|
|
|
|
while (FuncInfo.InsertPt != FuncInfo.MBB->end() &&
|
|
|
|
FuncInfo.InsertPt->getOpcode() == TargetOpcode::EH_LABEL)
|
|
|
|
++FuncInfo.InsertPt;
|
|
|
|
}
|
|
|
|
|
2011-11-29 20:40:47 +01:00
|
|
|
void FastISel::removeDeadCode(MachineBasicBlock::iterator I,
|
|
|
|
MachineBasicBlock::iterator E) {
|
|
|
|
assert (I && E && std::distance(I, E) > 0 && "Invalid iterator!");
|
|
|
|
while (I != E) {
|
|
|
|
MachineInstr *Dead = &*I;
|
|
|
|
++I;
|
|
|
|
Dead->eraseFromParent();
|
2013-03-08 23:56:31 +01:00
|
|
|
++NumFastIselDead;
|
2011-11-29 20:40:47 +01:00
|
|
|
}
|
|
|
|
recomputeInsertPt();
|
|
|
|
}
|
|
|
|
|
2012-10-03 10:10:01 +02:00
|
|
|
FastISel::SavePoint FastISel::enterLocalValueArea() {
|
2010-07-10 11:00:22 +02:00
|
|
|
MachineBasicBlock::iterator OldInsertPt = FuncInfo.InsertPt;
|
2014-02-18 23:05:46 +01:00
|
|
|
DebugLoc OldDL = DbgLoc;
|
2010-07-10 11:00:22 +02:00
|
|
|
recomputeInsertPt();
|
2014-02-18 23:05:46 +01:00
|
|
|
DbgLoc = DebugLoc();
|
2012-10-03 10:10:01 +02:00
|
|
|
SavePoint SP = { OldInsertPt, OldDL };
|
|
|
|
return SP;
|
2010-07-10 11:00:22 +02:00
|
|
|
}
|
|
|
|
|
2012-10-03 10:10:01 +02:00
|
|
|
void FastISel::leaveLocalValueArea(SavePoint OldInsertPt) {
|
2010-07-10 11:00:22 +02:00
|
|
|
if (FuncInfo.InsertPt != FuncInfo.MBB->begin())
|
2014-03-02 13:27:27 +01:00
|
|
|
LastLocalValue = std::prev(FuncInfo.InsertPt);
|
2010-07-10 11:00:22 +02:00
|
|
|
|
|
|
|
// Restore the previous insert position.
|
2012-10-03 10:10:01 +02:00
|
|
|
FuncInfo.InsertPt = OldInsertPt.InsertPt;
|
2014-02-18 23:05:46 +01:00
|
|
|
DbgLoc = OldInsertPt.DL;
|
2010-07-10 11:00:22 +02:00
|
|
|
}
|
|
|
|
|
2008-08-20 02:11:48 +02:00
|
|
|
/// SelectBinaryOp - Select and emit code for a binary operator instruction,
|
|
|
|
/// which has an opcode which directly corresponds to the given ISD opcode.
|
|
|
|
///
|
2010-04-15 03:51:59 +02:00
|
|
|
bool FastISel::SelectBinaryOp(const User *I, unsigned ISDOpcode) {
|
2009-08-11 00:56:29 +02:00
|
|
|
EVT VT = EVT::getEVT(I->getType(), /*HandleUnknown=*/true);
|
2009-08-11 22:47:22 +02:00
|
|
|
if (VT == MVT::Other || !VT.isSimple())
|
2008-08-21 03:41:07 +02:00
|
|
|
// Unhandled type. Halt "fast" selection and bail.
|
|
|
|
return false;
|
2008-09-05 20:44:22 +02:00
|
|
|
|
2008-08-26 22:52:40 +02:00
|
|
|
// We only handle legal types. For example, on x86-32 the instruction
|
|
|
|
// selector contains all of the 64-bit instructions from x86-64,
|
|
|
|
// under the assumption that i64 won't be used if the target doesn't
|
|
|
|
// support it.
|
2008-09-05 20:44:22 +02:00
|
|
|
if (!TLI.isTypeLegal(VT)) {
|
2009-08-11 22:47:22 +02:00
|
|
|
// MVT::i1 is special. Allow AND, OR, or XOR because they
|
2008-09-05 20:44:22 +02:00
|
|
|
// don't require additional zeroing, which makes them easy.
|
2009-08-11 22:47:22 +02:00
|
|
|
if (VT == MVT::i1 &&
|
2008-09-25 19:22:52 +02:00
|
|
|
(ISDOpcode == ISD::AND || ISDOpcode == ISD::OR ||
|
|
|
|
ISDOpcode == ISD::XOR))
|
2009-08-12 02:36:31 +02:00
|
|
|
VT = TLI.getTypeToTransformTo(I->getContext(), VT);
|
2008-09-05 20:44:22 +02:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
2008-08-21 03:41:07 +02:00
|
|
|
|
2011-04-17 03:16:47 +02:00
|
|
|
// Check if the first operand is a constant, and handle it as "ri". At -O0,
|
|
|
|
// we don't have anything that canonicalizes operand order.
|
|
|
|
if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(0)))
|
|
|
|
if (isa<Instruction>(I) && cast<Instruction>(I)->isCommutative()) {
|
|
|
|
unsigned Op1 = getRegForValue(I->getOperand(1));
|
|
|
|
if (Op1 == 0) return false;
|
|
|
|
|
|
|
|
bool Op1IsKill = hasTrivialKill(I->getOperand(1));
|
2011-04-23 01:38:06 +02:00
|
|
|
|
2011-04-17 22:23:29 +02:00
|
|
|
unsigned ResultReg = FastEmit_ri_(VT.getSimpleVT(), ISDOpcode, Op1,
|
|
|
|
Op1IsKill, CI->getZExtValue(),
|
|
|
|
VT.getSimpleVT());
|
|
|
|
if (ResultReg == 0) return false;
|
2011-04-23 01:38:06 +02:00
|
|
|
|
2011-04-17 22:23:29 +02:00
|
|
|
// We successfully emitted code for the given LLVM Instruction.
|
|
|
|
UpdateValueMap(I, ResultReg);
|
|
|
|
return true;
|
2011-04-17 03:16:47 +02:00
|
|
|
}
|
2011-04-23 01:38:06 +02:00
|
|
|
|
|
|
|
|
2008-09-04 01:12:08 +02:00
|
|
|
unsigned Op0 = getRegForValue(I->getOperand(0));
|
2011-04-17 22:23:29 +02:00
|
|
|
if (Op0 == 0) // Unhandled operand. Halt "fast" selection and bail.
|
2008-08-20 02:35:17 +02:00
|
|
|
return false;
|
|
|
|
|
2010-05-12 01:54:07 +02:00
|
|
|
bool Op0IsKill = hasTrivialKill(I->getOperand(0));
|
|
|
|
|
2008-08-21 03:41:07 +02:00
|
|
|
// Check if the second operand is a constant and handle it appropriately.
|
|
|
|
if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
|
2011-04-17 22:23:29 +02:00
|
|
|
uint64_t Imm = CI->getZExtValue();
|
2011-04-23 01:38:06 +02:00
|
|
|
|
2011-04-18 09:00:40 +02:00
|
|
|
// Transform "sdiv exact X, 8" -> "sra X, 3".
|
|
|
|
if (ISDOpcode == ISD::SDIV && isa<BinaryOperator>(I) &&
|
|
|
|
cast<BinaryOperator>(I)->isExact() &&
|
|
|
|
isPowerOf2_64(Imm)) {
|
|
|
|
Imm = Log2_64(Imm);
|
|
|
|
ISDOpcode = ISD::SRA;
|
|
|
|
}
|
2011-04-23 01:38:06 +02:00
|
|
|
|
2012-03-22 01:21:17 +01:00
|
|
|
// Transform "urem x, pow2" -> "and x, pow2-1".
|
|
|
|
if (ISDOpcode == ISD::UREM && isa<BinaryOperator>(I) &&
|
|
|
|
isPowerOf2_64(Imm)) {
|
|
|
|
--Imm;
|
|
|
|
ISDOpcode = ISD::AND;
|
|
|
|
}
|
|
|
|
|
2011-04-17 22:23:29 +02:00
|
|
|
unsigned ResultReg = FastEmit_ri_(VT.getSimpleVT(), ISDOpcode, Op0,
|
|
|
|
Op0IsKill, Imm, VT.getSimpleVT());
|
|
|
|
if (ResultReg == 0) return false;
|
2011-04-23 01:38:06 +02:00
|
|
|
|
2011-04-17 22:23:29 +02:00
|
|
|
// We successfully emitted code for the given LLVM Instruction.
|
|
|
|
UpdateValueMap(I, ResultReg);
|
|
|
|
return true;
|
2008-08-21 03:41:07 +02:00
|
|
|
}
|
|
|
|
|
2008-08-27 03:09:54 +02:00
|
|
|
// Check if the second operand is a constant float.
|
|
|
|
if (ConstantFP *CF = dyn_cast<ConstantFP>(I->getOperand(1))) {
|
2008-08-27 20:10:19 +02:00
|
|
|
unsigned ResultReg = FastEmit_rf(VT.getSimpleVT(), VT.getSimpleVT(),
|
2010-05-12 01:54:07 +02:00
|
|
|
ISDOpcode, Op0, Op0IsKill, CF);
|
2008-08-27 20:10:19 +02:00
|
|
|
if (ResultReg != 0) {
|
|
|
|
// We successfully emitted code for the given LLVM Instruction.
|
2008-09-04 01:12:08 +02:00
|
|
|
UpdateValueMap(I, ResultReg);
|
2008-08-27 20:10:19 +02:00
|
|
|
return true;
|
|
|
|
}
|
2008-08-27 03:09:54 +02:00
|
|
|
}
|
|
|
|
|
2008-09-04 01:12:08 +02:00
|
|
|
unsigned Op1 = getRegForValue(I->getOperand(1));
|
2008-08-21 03:41:07 +02:00
|
|
|
if (Op1 == 0)
|
|
|
|
// Unhandled operand. Halt "fast" selection and bail.
|
2008-08-20 02:11:48 +02:00
|
|
|
return false;
|
|
|
|
|
2010-05-12 01:54:07 +02:00
|
|
|
bool Op1IsKill = hasTrivialKill(I->getOperand(1));
|
|
|
|
|
2008-08-27 20:10:19 +02:00
|
|
|
// Now we have both operands in registers. Emit the instruction.
|
2008-08-26 01:58:18 +02:00
|
|
|
unsigned ResultReg = FastEmit_rr(VT.getSimpleVT(), VT.getSimpleVT(),
|
2010-05-12 01:54:07 +02:00
|
|
|
ISDOpcode,
|
|
|
|
Op0, Op0IsKill,
|
|
|
|
Op1, Op1IsKill);
|
2008-08-20 02:11:48 +02:00
|
|
|
if (ResultReg == 0)
|
|
|
|
// Target-specific code wasn't able to find a machine opcode for
|
|
|
|
// the given ISD opcode and type. Halt "fast" selection and bail.
|
|
|
|
return false;
|
|
|
|
|
2008-08-20 02:23:20 +02:00
|
|
|
// We successfully emitted code for the given LLVM Instruction.
|
2008-09-04 01:12:08 +02:00
|
|
|
UpdateValueMap(I, ResultReg);
|
2008-08-20 02:11:48 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-04-15 03:51:59 +02:00
|
|
|
bool FastISel::SelectGetElementPtr(const User *I) {
|
2008-09-04 01:12:08 +02:00
|
|
|
unsigned N = getRegForValue(I->getOperand(0));
|
2008-08-21 00:45:34 +02:00
|
|
|
if (N == 0)
|
|
|
|
// Unhandled operand. Halt "fast" selection and bail.
|
|
|
|
return false;
|
|
|
|
|
2010-05-12 01:54:07 +02:00
|
|
|
bool NIsKill = hasTrivialKill(I->getOperand(0));
|
|
|
|
|
2011-11-17 08:15:58 +01:00
|
|
|
// Keep a running tab of the total offset to coalesce multiple N = N + Offset
|
|
|
|
// into a single N = N + TotalOffset.
|
|
|
|
uint64_t TotalOffs = 0;
|
|
|
|
// FIXME: What's a good SWAG number for MaxOffs?
|
|
|
|
uint64_t MaxOffs = 2048;
|
2011-07-18 06:54:35 +02:00
|
|
|
Type *Ty = I->getOperand(0)->getType();
|
2009-08-11 22:47:22 +02:00
|
|
|
MVT VT = TLI.getPointerTy();
|
2010-04-15 03:51:59 +02:00
|
|
|
for (GetElementPtrInst::const_op_iterator OI = I->op_begin()+1,
|
|
|
|
E = I->op_end(); OI != E; ++OI) {
|
|
|
|
const Value *Idx = *OI;
|
2011-07-18 06:54:35 +02:00
|
|
|
if (StructType *StTy = dyn_cast<StructType>(Ty)) {
|
2008-08-21 00:45:34 +02:00
|
|
|
unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
|
|
|
|
if (Field) {
|
|
|
|
// N = N + Offset
|
2014-02-18 23:05:46 +01:00
|
|
|
TotalOffs += DL.getStructLayout(StTy)->getElementOffset(Field);
|
2011-11-17 08:15:58 +01:00
|
|
|
if (TotalOffs >= MaxOffs) {
|
|
|
|
N = FastEmit_ri_(VT, ISD::ADD, N, NIsKill, TotalOffs, VT);
|
|
|
|
if (N == 0)
|
|
|
|
// Unhandled operand. Halt "fast" selection and bail.
|
|
|
|
return false;
|
|
|
|
NIsKill = true;
|
|
|
|
TotalOffs = 0;
|
|
|
|
}
|
2008-08-21 00:45:34 +02:00
|
|
|
}
|
|
|
|
Ty = StTy->getElementType(Field);
|
|
|
|
} else {
|
|
|
|
Ty = cast<SequentialType>(Ty)->getElementType();
|
|
|
|
|
|
|
|
// If this is a constant subscript, handle it quickly.
|
2010-04-15 03:51:59 +02:00
|
|
|
if (const ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
|
2010-06-18 16:22:04 +02:00
|
|
|
if (CI->isZero()) continue;
|
2011-11-17 08:15:58 +01:00
|
|
|
// N = N + Offset
|
2012-07-06 19:44:22 +02:00
|
|
|
TotalOffs +=
|
2014-02-18 23:05:46 +01:00
|
|
|
DL.getTypeAllocSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
|
2011-11-17 08:15:58 +01:00
|
|
|
if (TotalOffs >= MaxOffs) {
|
|
|
|
N = FastEmit_ri_(VT, ISD::ADD, N, NIsKill, TotalOffs, VT);
|
|
|
|
if (N == 0)
|
|
|
|
// Unhandled operand. Halt "fast" selection and bail.
|
|
|
|
return false;
|
|
|
|
NIsKill = true;
|
|
|
|
TotalOffs = 0;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (TotalOffs) {
|
|
|
|
N = FastEmit_ri_(VT, ISD::ADD, N, NIsKill, TotalOffs, VT);
|
2008-08-21 00:45:34 +02:00
|
|
|
if (N == 0)
|
|
|
|
// Unhandled operand. Halt "fast" selection and bail.
|
|
|
|
return false;
|
2010-05-12 01:54:07 +02:00
|
|
|
NIsKill = true;
|
2011-11-17 08:15:58 +01:00
|
|
|
TotalOffs = 0;
|
2008-08-21 00:45:34 +02:00
|
|
|
}
|
2010-11-23 04:31:01 +01:00
|
|
|
|
2008-08-21 00:45:34 +02:00
|
|
|
// N = N + Idx * ElementSize;
|
2014-02-18 23:05:46 +01:00
|
|
|
uint64_t ElementSize = DL.getTypeAllocSize(Ty);
|
2010-05-12 01:54:07 +02:00
|
|
|
std::pair<unsigned, bool> Pair = getRegForGEPIndex(Idx);
|
|
|
|
unsigned IdxN = Pair.first;
|
|
|
|
bool IdxNIsKill = Pair.second;
|
2008-08-21 00:45:34 +02:00
|
|
|
if (IdxN == 0)
|
|
|
|
// Unhandled operand. Halt "fast" selection and bail.
|
|
|
|
return false;
|
|
|
|
|
2008-08-26 22:57:08 +02:00
|
|
|
if (ElementSize != 1) {
|
2010-05-12 01:54:07 +02:00
|
|
|
IdxN = FastEmit_ri_(VT, ISD::MUL, IdxN, IdxNIsKill, ElementSize, VT);
|
2008-08-26 22:57:08 +02:00
|
|
|
if (IdxN == 0)
|
|
|
|
// Unhandled operand. Halt "fast" selection and bail.
|
|
|
|
return false;
|
2010-05-12 01:54:07 +02:00
|
|
|
IdxNIsKill = true;
|
2008-08-26 22:57:08 +02:00
|
|
|
}
|
2010-05-12 01:54:07 +02:00
|
|
|
N = FastEmit_rr(VT, VT, ISD::ADD, N, NIsKill, IdxN, IdxNIsKill);
|
2008-08-21 00:45:34 +02:00
|
|
|
if (N == 0)
|
|
|
|
// Unhandled operand. Halt "fast" selection and bail.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2011-11-17 08:15:58 +01:00
|
|
|
if (TotalOffs) {
|
|
|
|
N = FastEmit_ri_(VT, ISD::ADD, N, NIsKill, TotalOffs, VT);
|
|
|
|
if (N == 0)
|
|
|
|
// Unhandled operand. Halt "fast" selection and bail.
|
|
|
|
return false;
|
|
|
|
}
|
2008-08-21 00:45:34 +02:00
|
|
|
|
|
|
|
// We successfully emitted code for the given LLVM Instruction.
|
2008-09-04 01:12:08 +02:00
|
|
|
UpdateValueMap(I, N);
|
2008-08-21 00:45:34 +02:00
|
|
|
return true;
|
2008-08-20 02:11:48 +02:00
|
|
|
}
|
|
|
|
|
2010-04-15 03:51:59 +02:00
|
|
|
bool FastISel::SelectCall(const User *I) {
|
2011-04-26 19:18:34 +02:00
|
|
|
const CallInst *Call = cast<CallInst>(I);
|
|
|
|
|
|
|
|
// Handle simple inline asms.
|
2011-10-12 17:56:56 +02:00
|
|
|
if (const InlineAsm *IA = dyn_cast<InlineAsm>(Call->getCalledValue())) {
|
2011-04-26 19:18:34 +02:00
|
|
|
// Don't attempt to handle constraints.
|
|
|
|
if (!IA->getConstraintString().empty())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
unsigned ExtraInfo = 0;
|
|
|
|
if (IA->hasSideEffects())
|
|
|
|
ExtraInfo |= InlineAsm::Extra_HasSideEffects;
|
|
|
|
if (IA->isAlignStack())
|
|
|
|
ExtraInfo |= InlineAsm::Extra_IsAlignStack;
|
|
|
|
|
2014-02-18 23:05:46 +01:00
|
|
|
BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
|
2011-04-26 19:18:34 +02:00
|
|
|
TII.get(TargetOpcode::INLINEASM))
|
|
|
|
.addExternalSymbol(IA->getAsmString().c_str())
|
|
|
|
.addImm(ExtraInfo);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-02-22 20:06:13 +01:00
|
|
|
MachineModuleInfo &MMI = FuncInfo.MF->getMMI();
|
|
|
|
ComputeUsesVAFloatArgument(*Call, &MMI);
|
|
|
|
|
2011-04-26 19:18:34 +02:00
|
|
|
const Function *F = Call->getCalledFunction();
|
2008-09-25 19:05:24 +02:00
|
|
|
if (!F) return false;
|
|
|
|
|
2010-04-13 19:07:06 +02:00
|
|
|
// Handle selected intrinsic function calls.
|
2011-04-19 07:52:03 +02:00
|
|
|
switch (F->getIntrinsicID()) {
|
2008-09-25 19:05:24 +02:00
|
|
|
default: break;
|
2012-05-12 01:21:01 +02:00
|
|
|
// At -O0 we don't care about the lifetime intrinsics.
|
2012-02-18 00:03:39 +01:00
|
|
|
case Intrinsic::lifetime_start:
|
|
|
|
case Intrinsic::lifetime_end:
|
2012-07-06 19:33:39 +02:00
|
|
|
// The donothing intrinsic does, well, nothing.
|
|
|
|
case Intrinsic::donothing:
|
2012-02-18 00:03:39 +01:00
|
|
|
return true;
|
2012-07-06 19:33:39 +02:00
|
|
|
|
2009-02-13 03:16:35 +01:00
|
|
|
case Intrinsic::dbg_declare: {
|
2011-04-26 19:18:34 +02:00
|
|
|
const DbgDeclareInst *DI = cast<DbgDeclareInst>(Call);
|
2013-06-28 07:43:10 +02:00
|
|
|
DIVariable DIVar(DI->getVariable());
|
2013-07-08 02:37:03 +02:00
|
|
|
assert((!DIVar || DIVar.isVariable()) &&
|
2013-06-28 07:43:10 +02:00
|
|
|
"Variable in DbgDeclareInst should be either null or a DIVariable.");
|
|
|
|
if (!DIVar ||
|
2012-03-15 22:33:44 +01:00
|
|
|
!FuncInfo.MF->getMMI().hasDebugInfo()) {
|
|
|
|
DEBUG(dbgs() << "Dropping debug info for " << *DI << "\n");
|
2009-07-03 00:43:26 +02:00
|
|
|
return true;
|
2012-03-15 22:33:44 +01:00
|
|
|
}
|
2009-02-13 03:16:35 +01:00
|
|
|
|
2010-04-15 03:51:59 +02:00
|
|
|
const Value *Address = DI->getAddress();
|
2012-03-15 22:33:47 +01:00
|
|
|
if (!Address || isa<UndefValue>(Address)) {
|
2012-03-15 22:33:44 +01:00
|
|
|
DEBUG(dbgs() << "Dropping debug info for " << *DI << "\n");
|
2010-02-06 03:26:02 +01:00
|
|
|
return true;
|
2012-03-15 22:33:44 +01:00
|
|
|
}
|
2010-09-14 22:29:31 +02:00
|
|
|
|
2013-07-09 22:28:37 +02:00
|
|
|
unsigned Offset = 0;
|
2013-06-16 22:34:15 +02:00
|
|
|
Optional<MachineOperand> Op;
|
|
|
|
if (const Argument *Arg = dyn_cast<Argument>(Address))
|
2011-09-09 00:59:09 +02:00
|
|
|
// Some arguments' frame index is recorded during argument lowering.
|
2013-07-09 22:28:37 +02:00
|
|
|
Offset = FuncInfo.getArgumentFrameIndex(Arg);
|
|
|
|
if (Offset)
|
|
|
|
Op = MachineOperand::CreateFI(Offset);
|
2013-06-16 22:34:15 +02:00
|
|
|
if (!Op)
|
|
|
|
if (unsigned Reg = lookUpRegForValue(Address))
|
|
|
|
Op = MachineOperand::CreateReg(Reg, false);
|
2012-03-20 02:07:58 +01:00
|
|
|
|
2012-03-30 02:02:55 +02:00
|
|
|
// If we have a VLA that has a "use" in a metadata node that's then used
|
|
|
|
// here but it has no other uses, then we have a problem. E.g.,
|
|
|
|
//
|
|
|
|
// int foo (const int *x) {
|
|
|
|
// char a[*x];
|
|
|
|
// return 0;
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// If we assign 'a' a vreg and fast isel later on has to use the selection
|
|
|
|
// DAG isel, it will want to copy the value to the vreg. However, there are
|
|
|
|
// no uses, which goes counter to what selection DAG isel expects.
|
2013-06-16 22:34:15 +02:00
|
|
|
if (!Op && !Address->use_empty() && isa<Instruction>(Address) &&
|
2012-03-20 02:07:58 +01:00
|
|
|
(!isa<AllocaInst>(Address) ||
|
|
|
|
!FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(Address))))
|
2013-06-16 22:34:15 +02:00
|
|
|
Op = MachineOperand::CreateReg(FuncInfo.InitializeRegForValue(Address),
|
2013-09-19 00:08:59 +02:00
|
|
|
false);
|
2013-06-16 22:34:15 +02:00
|
|
|
|
2013-09-19 00:08:59 +02:00
|
|
|
if (Op) {
|
2013-07-09 22:28:37 +02:00
|
|
|
if (Op->isReg()) {
|
|
|
|
Op->setIsDebug(true);
|
2014-02-18 23:05:46 +01:00
|
|
|
BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
|
2013-10-14 22:15:04 +02:00
|
|
|
TII.get(TargetOpcode::DBG_VALUE), false, Op->getReg(), 0,
|
|
|
|
DI->getVariable());
|
|
|
|
} else
|
2014-02-18 23:05:46 +01:00
|
|
|
BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
|
2013-10-14 22:15:04 +02:00
|
|
|
TII.get(TargetOpcode::DBG_VALUE))
|
|
|
|
.addOperand(*Op)
|
|
|
|
.addImm(0)
|
|
|
|
.addMetadata(DI->getVariable());
|
2013-09-19 00:08:59 +02:00
|
|
|
} else {
|
2012-03-20 02:07:53 +01:00
|
|
|
// We can't yet handle anything else here because it would require
|
|
|
|
// generating code, thus altering codegen because of debug info.
|
2013-05-22 20:02:19 +02:00
|
|
|
DEBUG(dbgs() << "Dropping debug info for " << *DI << "\n");
|
2013-09-19 00:08:59 +02:00
|
|
|
}
|
2008-09-25 19:05:24 +02:00
|
|
|
return true;
|
2009-02-13 03:16:35 +01:00
|
|
|
}
|
2010-02-26 21:01:55 +01:00
|
|
|
case Intrinsic::dbg_value: {
|
2010-04-07 03:15:14 +02:00
|
|
|
// This form of DBG_VALUE is target-independent.
|
2011-04-26 19:18:34 +02:00
|
|
|
const DbgValueInst *DI = cast<DbgValueInst>(Call);
|
2011-06-28 21:10:37 +02:00
|
|
|
const MCInstrDesc &II = TII.get(TargetOpcode::DBG_VALUE);
|
2010-04-15 03:51:59 +02:00
|
|
|
const Value *V = DI->getValue();
|
2010-02-26 21:01:55 +01:00
|
|
|
if (!V) {
|
|
|
|
// Currently the optimizer can produce this; insert an undef to
|
|
|
|
// help debugging. Probably the optimizer should not do this.
|
2014-02-18 23:05:46 +01:00
|
|
|
BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
|
2010-07-10 11:00:22 +02:00
|
|
|
.addReg(0U).addImm(DI->getOffset())
|
|
|
|
.addMetadata(DI->getVariable());
|
2010-04-15 03:51:59 +02:00
|
|
|
} else if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
|
2011-06-24 22:46:11 +02:00
|
|
|
if (CI->getBitWidth() > 64)
|
2014-02-18 23:05:46 +01:00
|
|
|
BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
|
2011-06-24 22:46:11 +02:00
|
|
|
.addCImm(CI).addImm(DI->getOffset())
|
|
|
|
.addMetadata(DI->getVariable());
|
2012-07-06 19:44:22 +02:00
|
|
|
else
|
2014-02-18 23:05:46 +01:00
|
|
|
BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
|
2011-06-24 22:46:11 +02:00
|
|
|
.addImm(CI->getZExtValue()).addImm(DI->getOffset())
|
|
|
|
.addMetadata(DI->getVariable());
|
2010-04-15 03:51:59 +02:00
|
|
|
} else if (const ConstantFP *CF = dyn_cast<ConstantFP>(V)) {
|
2014-02-18 23:05:46 +01:00
|
|
|
BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
|
2010-07-10 11:00:22 +02:00
|
|
|
.addFPImm(CF).addImm(DI->getOffset())
|
|
|
|
.addMetadata(DI->getVariable());
|
2010-02-26 21:01:55 +01:00
|
|
|
} else if (unsigned Reg = lookUpRegForValue(V)) {
|
2013-09-17 01:29:03 +02:00
|
|
|
// FIXME: This does not handle register-indirect values at offset 0.
|
2013-07-09 22:28:37 +02:00
|
|
|
bool IsIndirect = DI->getOffset() != 0;
|
2014-02-18 23:05:46 +01:00
|
|
|
BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, IsIndirect,
|
2013-07-09 22:28:37 +02:00
|
|
|
Reg, DI->getOffset(), DI->getVariable());
|
2010-02-26 21:01:55 +01:00
|
|
|
} else {
|
|
|
|
// We can't yet handle anything else here because it would require
|
|
|
|
// generating code, thus altering codegen because of debug info.
|
2013-05-22 20:02:19 +02:00
|
|
|
DEBUG(dbgs() << "Dropping debug info for " << *DI << "\n");
|
2010-11-23 04:31:01 +01:00
|
|
|
}
|
2010-02-26 21:01:55 +01:00
|
|
|
return true;
|
|
|
|
}
|
2011-05-14 02:47:51 +02:00
|
|
|
case Intrinsic::objectsize: {
|
|
|
|
ConstantInt *CI = cast<ConstantInt>(Call->getArgOperand(1));
|
|
|
|
unsigned long long Res = CI->isZero() ? -1ULL : 0;
|
|
|
|
Constant *ResCI = ConstantInt::get(Call->getType(), Res);
|
|
|
|
unsigned ResultReg = getRegForValue(ResCI);
|
|
|
|
if (ResultReg == 0)
|
|
|
|
return false;
|
|
|
|
UpdateValueMap(Call, ResultReg);
|
|
|
|
return true;
|
|
|
|
}
|
2013-03-07 21:42:17 +01:00
|
|
|
case Intrinsic::expect: {
|
2013-03-07 22:38:33 +01:00
|
|
|
unsigned ResultReg = getRegForValue(Call->getArgOperand(0));
|
2013-03-11 22:44:37 +01:00
|
|
|
if (ResultReg == 0)
|
|
|
|
return false;
|
2013-03-07 22:38:33 +01:00
|
|
|
UpdateValueMap(Call, ResultReg);
|
|
|
|
return true;
|
2013-03-07 21:42:17 +01:00
|
|
|
}
|
2008-09-25 19:05:24 +02:00
|
|
|
}
|
2010-04-13 19:07:06 +02:00
|
|
|
|
2011-08-19 00:06:10 +02:00
|
|
|
// Usually, it does not make sense to initialize a value,
|
|
|
|
// make an unrelated function call and use the value, because
|
|
|
|
// it tends to be spilled on the stack. So, we move the pointer
|
|
|
|
// to the last local value to the beginning of the block, so that
|
|
|
|
// all the values which have already been materialized,
|
|
|
|
// appear after the call. It also makes sense to skip intrinsics
|
|
|
|
// since they tend to be inlined.
|
2013-02-22 02:50:38 +01:00
|
|
|
if (!isa<IntrinsicInst>(Call))
|
2011-08-19 00:06:10 +02:00
|
|
|
flushLocalValueMap();
|
|
|
|
|
2010-04-13 19:07:06 +02:00
|
|
|
// An arbitrary call. Bail.
|
2008-09-25 19:05:24 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-04-15 03:51:59 +02:00
|
|
|
bool FastISel::SelectCast(const User *I, unsigned Opcode) {
|
2009-08-11 00:56:29 +02:00
|
|
|
EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
|
|
|
|
EVT DstVT = TLI.getValueType(I->getType());
|
2010-11-23 04:31:01 +01:00
|
|
|
|
2009-08-11 22:47:22 +02:00
|
|
|
if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
|
|
|
|
DstVT == MVT::Other || !DstVT.isSimple())
|
2008-08-27 01:46:32 +02:00
|
|
|
// Unhandled type. Halt "fast" selection and bail.
|
|
|
|
return false;
|
2010-11-23 04:31:01 +01:00
|
|
|
|
2011-05-26 01:49:02 +02:00
|
|
|
// Check if the destination type is legal.
|
2009-03-14 00:53:06 +01:00
|
|
|
if (!TLI.isTypeLegal(DstVT))
|
2011-05-26 01:49:02 +02:00
|
|
|
return false;
|
2009-03-14 00:53:06 +01:00
|
|
|
|
2011-05-26 01:49:02 +02:00
|
|
|
// Check if the source operand is legal.
|
2009-03-14 00:53:06 +01:00
|
|
|
if (!TLI.isTypeLegal(SrcVT))
|
2011-05-26 01:49:02 +02:00
|
|
|
return false;
|
2009-03-14 00:53:06 +01:00
|
|
|
|
2008-09-04 01:12:08 +02:00
|
|
|
unsigned InputReg = getRegForValue(I->getOperand(0));
|
2008-08-27 01:46:32 +02:00
|
|
|
if (!InputReg)
|
|
|
|
// Unhandled operand. Halt "fast" selection and bail.
|
|
|
|
return false;
|
2009-03-13 21:42:20 +01:00
|
|
|
|
2010-05-12 01:54:07 +02:00
|
|
|
bool InputRegIsKill = hasTrivialKill(I->getOperand(0));
|
|
|
|
|
2008-08-27 01:46:32 +02:00
|
|
|
unsigned ResultReg = FastEmit_r(SrcVT.getSimpleVT(),
|
|
|
|
DstVT.getSimpleVT(),
|
|
|
|
Opcode,
|
2010-05-12 01:54:07 +02:00
|
|
|
InputReg, InputRegIsKill);
|
2008-08-27 01:46:32 +02:00
|
|
|
if (!ResultReg)
|
|
|
|
return false;
|
2010-11-23 04:31:01 +01:00
|
|
|
|
2008-09-04 01:12:08 +02:00
|
|
|
UpdateValueMap(I, ResultReg);
|
2008-08-27 01:46:32 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-04-15 03:51:59 +02:00
|
|
|
bool FastISel::SelectBitCast(const User *I) {
|
2008-08-27 20:10:19 +02:00
|
|
|
// If the bitcast doesn't change the type, just use the operand value.
|
|
|
|
if (I->getType() == I->getOperand(0)->getType()) {
|
2008-09-04 01:12:08 +02:00
|
|
|
unsigned Reg = getRegForValue(I->getOperand(0));
|
2008-08-27 22:41:38 +02:00
|
|
|
if (Reg == 0)
|
|
|
|
return false;
|
2008-09-04 01:12:08 +02:00
|
|
|
UpdateValueMap(I, Reg);
|
2008-08-27 20:10:19 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-11-23 04:31:01 +01:00
|
|
|
// Bitcasts of other values become reg-reg copies or BITCAST operators.
|
2012-12-17 15:30:06 +01:00
|
|
|
EVT SrcEVT = TLI.getValueType(I->getOperand(0)->getType());
|
|
|
|
EVT DstEVT = TLI.getValueType(I->getType());
|
|
|
|
if (SrcEVT == MVT::Other || DstEVT == MVT::Other ||
|
|
|
|
!TLI.isTypeLegal(SrcEVT) || !TLI.isTypeLegal(DstEVT))
|
2008-08-27 01:46:32 +02:00
|
|
|
// Unhandled type. Halt "fast" selection and bail.
|
|
|
|
return false;
|
2010-11-23 04:31:01 +01:00
|
|
|
|
2012-12-17 15:30:06 +01:00
|
|
|
MVT SrcVT = SrcEVT.getSimpleVT();
|
|
|
|
MVT DstVT = DstEVT.getSimpleVT();
|
2008-09-04 01:12:08 +02:00
|
|
|
unsigned Op0 = getRegForValue(I->getOperand(0));
|
2008-08-27 20:10:19 +02:00
|
|
|
if (Op0 == 0)
|
|
|
|
// Unhandled operand. Halt "fast" selection and bail.
|
2008-08-27 01:46:32 +02:00
|
|
|
return false;
|
2010-05-12 01:54:07 +02:00
|
|
|
|
|
|
|
bool Op0IsKill = hasTrivialKill(I->getOperand(0));
|
2010-11-23 04:31:01 +01:00
|
|
|
|
2008-08-27 20:10:19 +02:00
|
|
|
// First, try to perform the bitcast by inserting a reg-reg copy.
|
|
|
|
unsigned ResultReg = 0;
|
2012-12-13 07:34:11 +01:00
|
|
|
if (SrcVT == DstVT) {
|
2012-02-22 06:59:10 +01:00
|
|
|
const TargetRegisterClass* SrcClass = TLI.getRegClassFor(SrcVT);
|
|
|
|
const TargetRegisterClass* DstClass = TLI.getRegClassFor(DstVT);
|
2010-07-11 07:16:54 +02:00
|
|
|
// Don't attempt a cross-class copy. It will likely fail.
|
|
|
|
if (SrcClass == DstClass) {
|
|
|
|
ResultReg = createResultReg(DstClass);
|
2014-02-18 23:05:46 +01:00
|
|
|
BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
|
|
|
|
TII.get(TargetOpcode::COPY), ResultReg).addReg(Op0);
|
2010-07-11 07:16:54 +02:00
|
|
|
}
|
2008-08-27 20:10:19 +02:00
|
|
|
}
|
2010-11-23 04:31:01 +01:00
|
|
|
|
|
|
|
// If the reg-reg copy failed, select a BITCAST opcode.
|
2008-08-27 20:10:19 +02:00
|
|
|
if (!ResultReg)
|
2012-12-13 07:34:11 +01:00
|
|
|
ResultReg = FastEmit_r(SrcVT, DstVT, ISD::BITCAST, Op0, Op0IsKill);
|
2010-11-23 04:31:01 +01:00
|
|
|
|
2008-08-27 20:10:19 +02:00
|
|
|
if (!ResultReg)
|
2008-08-27 01:46:32 +02:00
|
|
|
return false;
|
2010-11-23 04:31:01 +01:00
|
|
|
|
2008-09-04 01:12:08 +02:00
|
|
|
UpdateValueMap(I, ResultReg);
|
2008-08-27 01:46:32 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-09-04 01:12:08 +02:00
|
|
|
bool
|
2010-04-15 03:51:59 +02:00
|
|
|
FastISel::SelectInstruction(const Instruction *I) {
|
2010-04-23 17:29:50 +02:00
|
|
|
// Just before the terminator instruction, insert instructions to
|
|
|
|
// feed PHI nodes in successor blocks.
|
|
|
|
if (isa<TerminatorInst>(I))
|
|
|
|
if (!HandlePHINodesInSuccessorBlocks(I->getParent()))
|
|
|
|
return false;
|
|
|
|
|
2014-02-18 23:05:46 +01:00
|
|
|
DbgLoc = I->getDebugLoc();
|
2010-04-20 02:48:35 +02:00
|
|
|
|
2011-11-29 20:40:47 +01:00
|
|
|
MachineBasicBlock::iterator SavedInsertPt = FuncInfo.InsertPt;
|
|
|
|
|
2012-08-03 23:26:24 +02:00
|
|
|
// As a special case, don't handle calls to builtin library functions that
|
|
|
|
// may be translated directly to target instructions.
|
2012-08-03 06:06:28 +02:00
|
|
|
if (const CallInst *Call = dyn_cast<CallInst>(I)) {
|
|
|
|
const Function *F = Call->getCalledFunction();
|
|
|
|
LibFunc::Func Func;
|
|
|
|
if (F && !F->hasLocalLinkage() && F->hasName() &&
|
|
|
|
LibInfo->getLibFunc(F->getName(), Func) &&
|
2012-08-03 23:26:24 +02:00
|
|
|
LibInfo->hasOptimizedCodeGen(Func))
|
2012-08-03 06:06:28 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-12-05 02:27:58 +01:00
|
|
|
// First, try doing target-independent selection.
|
2013-02-27 20:54:00 +01:00
|
|
|
if (SelectOperator(I, I->getOpcode())) {
|
2013-03-08 23:56:31 +01:00
|
|
|
++NumFastIselSuccessIndependent;
|
2014-02-18 23:05:46 +01:00
|
|
|
DbgLoc = DebugLoc();
|
2009-12-05 02:27:58 +01:00
|
|
|
return true;
|
2010-04-20 02:48:35 +02:00
|
|
|
}
|
2012-07-06 19:44:22 +02:00
|
|
|
// Remove dead code. However, ignore call instructions since we've flushed
|
2011-11-29 20:40:47 +01:00
|
|
|
// the local value map and recomputed the insert point.
|
|
|
|
if (!isa<CallInst>(I)) {
|
|
|
|
recomputeInsertPt();
|
|
|
|
if (SavedInsertPt != FuncInfo.InsertPt)
|
|
|
|
removeDeadCode(FuncInfo.InsertPt, SavedInsertPt);
|
|
|
|
}
|
2009-12-05 02:27:58 +01:00
|
|
|
|
|
|
|
// Next, try calling the target to attempt to handle the instruction.
|
2011-11-29 20:40:47 +01:00
|
|
|
SavedInsertPt = FuncInfo.InsertPt;
|
2010-04-20 02:48:35 +02:00
|
|
|
if (TargetSelectInstruction(I)) {
|
2013-03-08 23:56:31 +01:00
|
|
|
++NumFastIselSuccessTarget;
|
2014-02-18 23:05:46 +01:00
|
|
|
DbgLoc = DebugLoc();
|
2009-12-05 02:27:58 +01:00
|
|
|
return true;
|
2010-04-20 02:48:35 +02:00
|
|
|
}
|
2011-11-29 20:40:47 +01:00
|
|
|
// Check for dead code and remove as necessary.
|
|
|
|
recomputeInsertPt();
|
|
|
|
if (SavedInsertPt != FuncInfo.InsertPt)
|
|
|
|
removeDeadCode(FuncInfo.InsertPt, SavedInsertPt);
|
2009-12-05 02:27:58 +01:00
|
|
|
|
2014-02-18 23:05:46 +01:00
|
|
|
DbgLoc = DebugLoc();
|
2009-12-05 02:27:58 +01:00
|
|
|
return false;
|
2008-09-05 20:18:20 +02:00
|
|
|
}
|
|
|
|
|
2008-10-03 00:15:21 +02:00
|
|
|
/// FastEmitBranch - Emit an unconditional branch to the given block,
|
|
|
|
/// unless it is the immediate (fall-through) successor, and update
|
|
|
|
/// the CFG.
|
|
|
|
void
|
2014-02-18 23:05:46 +01:00
|
|
|
FastISel::FastEmitBranch(MachineBasicBlock *MSucc, DebugLoc DbgLoc) {
|
2012-04-10 20:18:10 +02:00
|
|
|
|
2013-02-11 02:27:15 +01:00
|
|
|
if (FuncInfo.MBB->getBasicBlock()->size() > 1 &&
|
|
|
|
FuncInfo.MBB->isLayoutSuccessor(MSucc)) {
|
2012-04-10 20:18:10 +02:00
|
|
|
// For more accurate line information if this is the only instruction
|
|
|
|
// in the block then emit it, otherwise we have the unconditional
|
|
|
|
// fall-through case, which needs no instructions.
|
2008-10-03 00:15:21 +02:00
|
|
|
} else {
|
|
|
|
// The unconditional branch case.
|
2014-04-14 02:51:57 +02:00
|
|
|
TII.InsertBranch(*FuncInfo.MBB, MSucc, nullptr,
|
2014-02-18 23:05:46 +01:00
|
|
|
SmallVector<MachineOperand, 0>(), DbgLoc);
|
2008-10-03 00:15:21 +02:00
|
|
|
}
|
2010-07-10 11:00:22 +02:00
|
|
|
FuncInfo.MBB->addSuccessor(MSucc);
|
2008-10-03 00:15:21 +02:00
|
|
|
}
|
|
|
|
|
2009-09-04 00:53:57 +02:00
|
|
|
/// SelectFNeg - Emit an FNeg operation.
|
|
|
|
///
|
|
|
|
bool
|
2010-04-15 03:51:59 +02:00
|
|
|
FastISel::SelectFNeg(const User *I) {
|
2009-09-04 00:53:57 +02:00
|
|
|
unsigned OpReg = getRegForValue(BinaryOperator::getFNegArgument(I));
|
|
|
|
if (OpReg == 0) return false;
|
|
|
|
|
2010-05-12 01:54:07 +02:00
|
|
|
bool OpRegIsKill = hasTrivialKill(I);
|
|
|
|
|
2009-09-11 02:36:43 +02:00
|
|
|
// If the target has ISD::FNEG, use it.
|
|
|
|
EVT VT = TLI.getValueType(I->getType());
|
|
|
|
unsigned ResultReg = FastEmit_r(VT.getSimpleVT(), VT.getSimpleVT(),
|
2010-05-12 01:54:07 +02:00
|
|
|
ISD::FNEG, OpReg, OpRegIsKill);
|
2009-09-11 02:36:43 +02:00
|
|
|
if (ResultReg != 0) {
|
|
|
|
UpdateValueMap(I, ResultReg);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-09-11 02:34:46 +02:00
|
|
|
// Bitcast the value to integer, twiddle the sign bit with xor,
|
|
|
|
// and then bitcast it back to floating-point.
|
2009-09-04 00:53:57 +02:00
|
|
|
if (VT.getSizeInBits() > 64) return false;
|
2009-09-11 02:34:46 +02:00
|
|
|
EVT IntVT = EVT::getIntegerVT(I->getContext(), VT.getSizeInBits());
|
|
|
|
if (!TLI.isTypeLegal(IntVT))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
unsigned IntReg = FastEmit_r(VT.getSimpleVT(), IntVT.getSimpleVT(),
|
2010-11-23 04:31:01 +01:00
|
|
|
ISD::BITCAST, OpReg, OpRegIsKill);
|
2009-09-11 02:34:46 +02:00
|
|
|
if (IntReg == 0)
|
|
|
|
return false;
|
|
|
|
|
2010-05-12 01:54:07 +02:00
|
|
|
unsigned IntResultReg = FastEmit_ri_(IntVT.getSimpleVT(), ISD::XOR,
|
|
|
|
IntReg, /*Kill=*/true,
|
2009-09-11 02:34:46 +02:00
|
|
|
UINT64_C(1) << (VT.getSizeInBits()-1),
|
|
|
|
IntVT.getSimpleVT());
|
|
|
|
if (IntResultReg == 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
ResultReg = FastEmit_r(IntVT.getSimpleVT(), VT.getSimpleVT(),
|
2010-11-23 04:31:01 +01:00
|
|
|
ISD::BITCAST, IntResultReg, /*Kill=*/true);
|
2009-09-04 00:53:57 +02:00
|
|
|
if (ResultReg == 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
UpdateValueMap(I, ResultReg);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-05-16 22:27:46 +02:00
|
|
|
bool
|
|
|
|
FastISel::SelectExtractValue(const User *U) {
|
|
|
|
const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(U);
|
2011-05-16 22:34:53 +02:00
|
|
|
if (!EVI)
|
2011-05-16 22:27:46 +02:00
|
|
|
return false;
|
|
|
|
|
2011-05-16 23:06:17 +02:00
|
|
|
// Make sure we only try to handle extracts with a legal result. But also
|
|
|
|
// allow i1 because it's easy.
|
2011-05-16 22:27:46 +02:00
|
|
|
EVT RealVT = TLI.getValueType(EVI->getType(), /*AllowUnknown=*/true);
|
|
|
|
if (!RealVT.isSimple())
|
|
|
|
return false;
|
|
|
|
MVT VT = RealVT.getSimpleVT();
|
2011-05-16 23:06:17 +02:00
|
|
|
if (!TLI.isTypeLegal(VT) && VT != MVT::i1)
|
2011-05-16 22:27:46 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
const Value *Op0 = EVI->getOperand(0);
|
2011-07-18 06:54:35 +02:00
|
|
|
Type *AggTy = Op0->getType();
|
2011-05-16 22:27:46 +02:00
|
|
|
|
|
|
|
// Get the base result register.
|
|
|
|
unsigned ResultReg;
|
|
|
|
DenseMap<const Value *, unsigned>::iterator I = FuncInfo.ValueMap.find(Op0);
|
|
|
|
if (I != FuncInfo.ValueMap.end())
|
|
|
|
ResultReg = I->second;
|
2011-06-06 07:46:34 +02:00
|
|
|
else if (isa<Instruction>(Op0))
|
2011-05-16 22:27:46 +02:00
|
|
|
ResultReg = FuncInfo.InitializeRegForValue(Op0);
|
2011-06-06 07:46:34 +02:00
|
|
|
else
|
|
|
|
return false; // fast-isel can't handle aggregate constants at the moment
|
2011-05-16 22:27:46 +02:00
|
|
|
|
|
|
|
// Get the actual result register, which is an offset from the base register.
|
2011-07-13 12:26:04 +02:00
|
|
|
unsigned VTIndex = ComputeLinearIndex(AggTy, EVI->getIndices());
|
2011-05-16 22:27:46 +02:00
|
|
|
|
|
|
|
SmallVector<EVT, 4> AggValueVTs;
|
|
|
|
ComputeValueVTs(TLI, AggTy, AggValueVTs);
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < VTIndex; i++)
|
|
|
|
ResultReg += TLI.getNumRegisters(FuncInfo.Fn->getContext(), AggValueVTs[i]);
|
|
|
|
|
|
|
|
UpdateValueMap(EVI, ResultReg);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-09-05 20:18:20 +02:00
|
|
|
bool
|
2010-04-15 03:51:59 +02:00
|
|
|
FastISel::SelectOperator(const User *I, unsigned Opcode) {
|
2008-09-05 20:18:20 +02:00
|
|
|
switch (Opcode) {
|
2009-06-05 00:49:04 +02:00
|
|
|
case Instruction::Add:
|
|
|
|
return SelectBinaryOp(I, ISD::ADD);
|
|
|
|
case Instruction::FAdd:
|
|
|
|
return SelectBinaryOp(I, ISD::FADD);
|
|
|
|
case Instruction::Sub:
|
|
|
|
return SelectBinaryOp(I, ISD::SUB);
|
|
|
|
case Instruction::FSub:
|
2009-09-04 00:53:57 +02:00
|
|
|
// FNeg is currently represented in LLVM IR as a special case of FSub.
|
|
|
|
if (BinaryOperator::isFNeg(I))
|
|
|
|
return SelectFNeg(I);
|
2009-06-05 00:49:04 +02:00
|
|
|
return SelectBinaryOp(I, ISD::FSUB);
|
|
|
|
case Instruction::Mul:
|
|
|
|
return SelectBinaryOp(I, ISD::MUL);
|
|
|
|
case Instruction::FMul:
|
|
|
|
return SelectBinaryOp(I, ISD::FMUL);
|
2008-09-04 01:12:08 +02:00
|
|
|
case Instruction::SDiv:
|
|
|
|
return SelectBinaryOp(I, ISD::SDIV);
|
|
|
|
case Instruction::UDiv:
|
|
|
|
return SelectBinaryOp(I, ISD::UDIV);
|
|
|
|
case Instruction::FDiv:
|
|
|
|
return SelectBinaryOp(I, ISD::FDIV);
|
|
|
|
case Instruction::SRem:
|
|
|
|
return SelectBinaryOp(I, ISD::SREM);
|
|
|
|
case Instruction::URem:
|
|
|
|
return SelectBinaryOp(I, ISD::UREM);
|
|
|
|
case Instruction::FRem:
|
|
|
|
return SelectBinaryOp(I, ISD::FREM);
|
|
|
|
case Instruction::Shl:
|
|
|
|
return SelectBinaryOp(I, ISD::SHL);
|
|
|
|
case Instruction::LShr:
|
|
|
|
return SelectBinaryOp(I, ISD::SRL);
|
|
|
|
case Instruction::AShr:
|
|
|
|
return SelectBinaryOp(I, ISD::SRA);
|
|
|
|
case Instruction::And:
|
|
|
|
return SelectBinaryOp(I, ISD::AND);
|
|
|
|
case Instruction::Or:
|
|
|
|
return SelectBinaryOp(I, ISD::OR);
|
|
|
|
case Instruction::Xor:
|
|
|
|
return SelectBinaryOp(I, ISD::XOR);
|
|
|
|
|
|
|
|
case Instruction::GetElementPtr:
|
|
|
|
return SelectGetElementPtr(I);
|
|
|
|
|
|
|
|
case Instruction::Br: {
|
2010-04-15 03:51:59 +02:00
|
|
|
const BranchInst *BI = cast<BranchInst>(I);
|
2008-09-04 01:12:08 +02:00
|
|
|
|
|
|
|
if (BI->isUnconditional()) {
|
2010-04-15 03:51:59 +02:00
|
|
|
const BasicBlock *LLVMSucc = BI->getSuccessor(0);
|
2010-07-07 18:29:44 +02:00
|
|
|
MachineBasicBlock *MSucc = FuncInfo.MBBMap[LLVMSucc];
|
2010-06-18 00:43:56 +02:00
|
|
|
FastEmitBranch(MSucc, BI->getDebugLoc());
|
2008-09-04 01:12:08 +02:00
|
|
|
return true;
|
2008-08-27 02:31:01 +02:00
|
|
|
}
|
2008-09-04 01:12:08 +02:00
|
|
|
|
|
|
|
// Conditional branches are not handed yet.
|
|
|
|
// Halt "fast" selection and bail.
|
|
|
|
return false;
|
2008-08-13 22:19:35 +02:00
|
|
|
}
|
|
|
|
|
2008-09-05 03:08:41 +02:00
|
|
|
case Instruction::Unreachable:
|
|
|
|
// Nothing to emit.
|
|
|
|
return true;
|
|
|
|
|
2008-09-10 22:11:02 +02:00
|
|
|
case Instruction::Alloca:
|
|
|
|
// FunctionLowering has the static-sized case covered.
|
2010-07-07 18:29:44 +02:00
|
|
|
if (FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(I)))
|
2008-09-10 22:11:02 +02:00
|
|
|
return true;
|
|
|
|
|
|
|
|
// Dynamic-sized alloca is not handled yet.
|
|
|
|
return false;
|
2010-11-23 04:31:01 +01:00
|
|
|
|
2008-09-25 19:05:24 +02:00
|
|
|
case Instruction::Call:
|
|
|
|
return SelectCall(I);
|
2010-11-23 04:31:01 +01:00
|
|
|
|
2008-09-04 01:12:08 +02:00
|
|
|
case Instruction::BitCast:
|
|
|
|
return SelectBitCast(I);
|
|
|
|
|
|
|
|
case Instruction::FPToSI:
|
|
|
|
return SelectCast(I, ISD::FP_TO_SINT);
|
|
|
|
case Instruction::ZExt:
|
|
|
|
return SelectCast(I, ISD::ZERO_EXTEND);
|
|
|
|
case Instruction::SExt:
|
|
|
|
return SelectCast(I, ISD::SIGN_EXTEND);
|
|
|
|
case Instruction::Trunc:
|
|
|
|
return SelectCast(I, ISD::TRUNCATE);
|
|
|
|
case Instruction::SIToFP:
|
|
|
|
return SelectCast(I, ISD::SINT_TO_FP);
|
|
|
|
|
|
|
|
case Instruction::IntToPtr: // Deliberate fall-through.
|
|
|
|
case Instruction::PtrToInt: {
|
2009-08-11 00:56:29 +02:00
|
|
|
EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
|
|
|
|
EVT DstVT = TLI.getValueType(I->getType());
|
2008-09-04 01:12:08 +02:00
|
|
|
if (DstVT.bitsGT(SrcVT))
|
|
|
|
return SelectCast(I, ISD::ZERO_EXTEND);
|
|
|
|
if (DstVT.bitsLT(SrcVT))
|
|
|
|
return SelectCast(I, ISD::TRUNCATE);
|
|
|
|
unsigned Reg = getRegForValue(I->getOperand(0));
|
|
|
|
if (Reg == 0) return false;
|
|
|
|
UpdateValueMap(I, Reg);
|
|
|
|
return true;
|
|
|
|
}
|
2008-09-23 23:53:34 +02:00
|
|
|
|
2011-05-16 22:27:46 +02:00
|
|
|
case Instruction::ExtractValue:
|
|
|
|
return SelectExtractValue(I);
|
|
|
|
|
2010-04-20 17:00:41 +02:00
|
|
|
case Instruction::PHI:
|
|
|
|
llvm_unreachable("FastISel shouldn't visit PHI nodes!");
|
|
|
|
|
2008-09-04 01:12:08 +02:00
|
|
|
default:
|
|
|
|
// Unhandled instruction. Halt "fast" selection and bail.
|
|
|
|
return false;
|
|
|
|
}
|
2008-08-13 22:19:35 +02:00
|
|
|
}
|
|
|
|
|
2012-08-03 06:06:28 +02:00
|
|
|
FastISel::FastISel(FunctionLoweringInfo &funcInfo,
|
|
|
|
const TargetLibraryInfo *libInfo)
|
2010-07-10 11:00:22 +02:00
|
|
|
: FuncInfo(funcInfo),
|
2010-07-07 18:29:44 +02:00
|
|
|
MRI(FuncInfo.MF->getRegInfo()),
|
|
|
|
MFI(*FuncInfo.MF->getFrameInfo()),
|
|
|
|
MCP(*FuncInfo.MF->getConstantPool()),
|
|
|
|
TM(FuncInfo.MF->getTarget()),
|
2014-02-18 23:05:46 +01:00
|
|
|
DL(*TM.getDataLayout()),
|
2008-08-22 02:20:26 +02:00
|
|
|
TII(*TM.getInstrInfo()),
|
2010-05-06 01:58:35 +02:00
|
|
|
TLI(*TM.getTargetLowering()),
|
2012-08-03 06:06:28 +02:00
|
|
|
TRI(*TM.getRegisterInfo()),
|
|
|
|
LibInfo(libInfo) {
|
2008-08-20 23:05:57 +02:00
|
|
|
}
|
|
|
|
|
2008-08-14 23:51:29 +02:00
|
|
|
FastISel::~FastISel() {}
|
|
|
|
|
2013-02-11 02:27:15 +01:00
|
|
|
bool FastISel::FastLowerArguments() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-08-11 22:47:22 +02:00
|
|
|
unsigned FastISel::FastEmit_(MVT, MVT,
|
2010-01-05 23:26:32 +01:00
|
|
|
unsigned) {
|
2008-08-13 22:19:35 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-08-11 22:47:22 +02:00
|
|
|
unsigned FastISel::FastEmit_r(MVT, MVT,
|
2010-05-12 01:54:07 +02:00
|
|
|
unsigned,
|
|
|
|
unsigned /*Op0*/, bool /*Op0IsKill*/) {
|
2008-08-13 22:19:35 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-11-23 04:31:01 +01:00
|
|
|
unsigned FastISel::FastEmit_rr(MVT, MVT,
|
2010-05-12 01:54:07 +02:00
|
|
|
unsigned,
|
|
|
|
unsigned /*Op0*/, bool /*Op0IsKill*/,
|
|
|
|
unsigned /*Op1*/, bool /*Op1IsKill*/) {
|
2008-08-13 22:19:35 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-01-05 23:26:32 +01:00
|
|
|
unsigned FastISel::FastEmit_i(MVT, MVT, unsigned, uint64_t /*Imm*/) {
|
2008-08-21 00:45:34 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-08-11 22:47:22 +02:00
|
|
|
unsigned FastISel::FastEmit_f(MVT, MVT,
|
2010-04-15 03:51:59 +02:00
|
|
|
unsigned, const ConstantFP * /*FPImm*/) {
|
2008-08-27 03:09:54 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-08-11 22:47:22 +02:00
|
|
|
unsigned FastISel::FastEmit_ri(MVT, MVT,
|
2010-05-12 01:54:07 +02:00
|
|
|
unsigned,
|
|
|
|
unsigned /*Op0*/, bool /*Op0IsKill*/,
|
2008-08-26 01:58:18 +02:00
|
|
|
uint64_t /*Imm*/) {
|
2008-08-21 03:41:07 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-08-11 22:47:22 +02:00
|
|
|
unsigned FastISel::FastEmit_rf(MVT, MVT,
|
2010-05-12 01:54:07 +02:00
|
|
|
unsigned,
|
|
|
|
unsigned /*Op0*/, bool /*Op0IsKill*/,
|
2010-04-15 03:51:59 +02:00
|
|
|
const ConstantFP * /*FPImm*/) {
|
2008-08-27 03:09:54 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-08-11 22:47:22 +02:00
|
|
|
unsigned FastISel::FastEmit_rri(MVT, MVT,
|
2010-01-05 23:26:32 +01:00
|
|
|
unsigned,
|
2010-05-12 01:54:07 +02:00
|
|
|
unsigned /*Op0*/, bool /*Op0IsKill*/,
|
|
|
|
unsigned /*Op1*/, bool /*Op1IsKill*/,
|
2008-08-21 03:41:07 +02:00
|
|
|
uint64_t /*Imm*/) {
|
2008-08-21 00:45:34 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// FastEmit_ri_ - This method is a wrapper of FastEmit_ri. It first tries
|
|
|
|
/// to emit an instruction with an immediate operand using FastEmit_ri.
|
|
|
|
/// If that fails, it materializes the immediate into a register and try
|
|
|
|
/// FastEmit_rr instead.
|
2010-01-05 23:26:32 +01:00
|
|
|
unsigned FastISel::FastEmit_ri_(MVT VT, unsigned Opcode,
|
2010-05-12 01:54:07 +02:00
|
|
|
unsigned Op0, bool Op0IsKill,
|
|
|
|
uint64_t Imm, MVT ImmType) {
|
2011-04-17 22:23:29 +02:00
|
|
|
// If this is a multiply by a power of two, emit this as a shift left.
|
|
|
|
if (Opcode == ISD::MUL && isPowerOf2_64(Imm)) {
|
|
|
|
Opcode = ISD::SHL;
|
|
|
|
Imm = Log2_64(Imm);
|
2011-04-18 08:55:51 +02:00
|
|
|
} else if (Opcode == ISD::UDIV && isPowerOf2_64(Imm)) {
|
|
|
|
// div x, 8 -> srl x, 3
|
|
|
|
Opcode = ISD::SRL;
|
|
|
|
Imm = Log2_64(Imm);
|
2011-04-17 22:23:29 +02:00
|
|
|
}
|
2011-04-23 01:38:06 +02:00
|
|
|
|
2011-04-17 22:23:29 +02:00
|
|
|
// Horrible hack (to be removed), check to make sure shift amounts are
|
|
|
|
// in-range.
|
|
|
|
if ((Opcode == ISD::SHL || Opcode == ISD::SRA || Opcode == ISD::SRL) &&
|
|
|
|
Imm >= VT.getSizeInBits())
|
|
|
|
return 0;
|
2011-04-23 01:38:06 +02:00
|
|
|
|
2008-08-21 00:45:34 +02:00
|
|
|
// First check if immediate type is legal. If not, we can't use the ri form.
|
2010-05-12 01:54:07 +02:00
|
|
|
unsigned ResultReg = FastEmit_ri(VT, VT, Opcode, Op0, Op0IsKill, Imm);
|
2008-08-21 00:45:34 +02:00
|
|
|
if (ResultReg != 0)
|
|
|
|
return ResultReg;
|
2008-08-26 01:58:18 +02:00
|
|
|
unsigned MaterialReg = FastEmit_i(ImmType, ImmType, ISD::Constant, Imm);
|
2011-04-30 01:34:52 +02:00
|
|
|
if (MaterialReg == 0) {
|
|
|
|
// This is a bit ugly/slow, but failing here means falling out of
|
|
|
|
// fast-isel, which would be very slow.
|
2011-07-18 06:54:35 +02:00
|
|
|
IntegerType *ITy = IntegerType::get(FuncInfo.Fn->getContext(),
|
2011-04-30 01:34:52 +02:00
|
|
|
VT.getSizeInBits());
|
|
|
|
MaterialReg = getRegForValue(ConstantInt::get(ITy, Imm));
|
2013-03-29 00:04:47 +01:00
|
|
|
assert (MaterialReg != 0 && "Unable to materialize imm.");
|
|
|
|
if (MaterialReg == 0) return 0;
|
2011-04-30 01:34:52 +02:00
|
|
|
}
|
2010-05-12 01:54:07 +02:00
|
|
|
return FastEmit_rr(VT, VT, Opcode,
|
|
|
|
Op0, Op0IsKill,
|
|
|
|
MaterialReg, /*Kill=*/true);
|
2008-08-21 03:41:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned FastISel::createResultReg(const TargetRegisterClass* RC) {
|
|
|
|
return MRI.createVirtualRegister(RC);
|
2008-08-21 00:45:34 +02:00
|
|
|
}
|
|
|
|
|
2008-08-13 22:19:35 +02:00
|
|
|
unsigned FastISel::FastEmitInst_(unsigned MachineInstOpcode,
|
2008-08-20 20:09:38 +02:00
|
|
|
const TargetRegisterClass* RC) {
|
2008-08-21 03:41:07 +02:00
|
|
|
unsigned ResultReg = createResultReg(RC);
|
2011-06-28 21:10:37 +02:00
|
|
|
const MCInstrDesc &II = TII.get(MachineInstOpcode);
|
2008-08-13 22:19:35 +02:00
|
|
|
|
2014-02-18 23:05:46 +01:00
|
|
|
BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg);
|
2008-08-13 22:19:35 +02:00
|
|
|
return ResultReg;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned FastISel::FastEmitInst_r(unsigned MachineInstOpcode,
|
|
|
|
const TargetRegisterClass *RC,
|
2010-05-12 01:54:07 +02:00
|
|
|
unsigned Op0, bool Op0IsKill) {
|
2008-08-21 03:41:07 +02:00
|
|
|
unsigned ResultReg = createResultReg(RC);
|
2011-06-28 21:10:37 +02:00
|
|
|
const MCInstrDesc &II = TII.get(MachineInstOpcode);
|
2008-08-13 22:19:35 +02:00
|
|
|
|
2008-09-08 10:38:20 +02:00
|
|
|
if (II.getNumDefs() >= 1)
|
2014-02-18 23:05:46 +01:00
|
|
|
BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
|
2010-07-10 11:00:22 +02:00
|
|
|
.addReg(Op0, Op0IsKill * RegState::Kill);
|
2008-09-08 10:38:20 +02:00
|
|
|
else {
|
2014-02-18 23:05:46 +01:00
|
|
|
BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
|
2010-07-10 11:00:22 +02:00
|
|
|
.addReg(Op0, Op0IsKill * RegState::Kill);
|
2014-02-18 23:05:46 +01:00
|
|
|
BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
|
|
|
|
TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]);
|
2008-09-08 10:38:20 +02:00
|
|
|
}
|
|
|
|
|
2008-08-13 22:19:35 +02:00
|
|
|
return ResultReg;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned FastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
|
|
|
|
const TargetRegisterClass *RC,
|
2010-05-12 01:54:07 +02:00
|
|
|
unsigned Op0, bool Op0IsKill,
|
|
|
|
unsigned Op1, bool Op1IsKill) {
|
2008-08-21 03:41:07 +02:00
|
|
|
unsigned ResultReg = createResultReg(RC);
|
2011-06-28 21:10:37 +02:00
|
|
|
const MCInstrDesc &II = TII.get(MachineInstOpcode);
|
2008-08-13 22:19:35 +02:00
|
|
|
|
2008-09-08 10:38:20 +02:00
|
|
|
if (II.getNumDefs() >= 1)
|
2014-02-18 23:05:46 +01:00
|
|
|
BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
|
2010-05-12 01:54:07 +02:00
|
|
|
.addReg(Op0, Op0IsKill * RegState::Kill)
|
|
|
|
.addReg(Op1, Op1IsKill * RegState::Kill);
|
2008-09-08 10:38:20 +02:00
|
|
|
else {
|
2014-02-18 23:05:46 +01:00
|
|
|
BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
|
2010-05-12 01:54:07 +02:00
|
|
|
.addReg(Op0, Op0IsKill * RegState::Kill)
|
|
|
|
.addReg(Op1, Op1IsKill * RegState::Kill);
|
2014-02-18 23:05:46 +01:00
|
|
|
BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
|
|
|
|
TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]);
|
2008-09-08 10:38:20 +02:00
|
|
|
}
|
2008-08-13 22:19:35 +02:00
|
|
|
return ResultReg;
|
|
|
|
}
|
2008-08-21 03:41:07 +02:00
|
|
|
|
2011-05-05 19:59:04 +02:00
|
|
|
unsigned FastISel::FastEmitInst_rrr(unsigned MachineInstOpcode,
|
|
|
|
const TargetRegisterClass *RC,
|
|
|
|
unsigned Op0, bool Op0IsKill,
|
|
|
|
unsigned Op1, bool Op1IsKill,
|
|
|
|
unsigned Op2, bool Op2IsKill) {
|
|
|
|
unsigned ResultReg = createResultReg(RC);
|
2011-06-28 21:10:37 +02:00
|
|
|
const MCInstrDesc &II = TII.get(MachineInstOpcode);
|
2011-05-05 19:59:04 +02:00
|
|
|
|
|
|
|
if (II.getNumDefs() >= 1)
|
2014-02-18 23:05:46 +01:00
|
|
|
BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
|
2011-05-05 19:59:04 +02:00
|
|
|
.addReg(Op0, Op0IsKill * RegState::Kill)
|
|
|
|
.addReg(Op1, Op1IsKill * RegState::Kill)
|
|
|
|
.addReg(Op2, Op2IsKill * RegState::Kill);
|
|
|
|
else {
|
2014-02-18 23:05:46 +01:00
|
|
|
BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
|
2011-05-05 19:59:04 +02:00
|
|
|
.addReg(Op0, Op0IsKill * RegState::Kill)
|
|
|
|
.addReg(Op1, Op1IsKill * RegState::Kill)
|
|
|
|
.addReg(Op2, Op2IsKill * RegState::Kill);
|
2014-02-18 23:05:46 +01:00
|
|
|
BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
|
|
|
|
TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]);
|
2011-05-05 19:59:04 +02:00
|
|
|
}
|
|
|
|
return ResultReg;
|
|
|
|
}
|
|
|
|
|
2008-08-21 03:41:07 +02:00
|
|
|
unsigned FastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
|
|
|
|
const TargetRegisterClass *RC,
|
2010-05-12 01:54:07 +02:00
|
|
|
unsigned Op0, bool Op0IsKill,
|
|
|
|
uint64_t Imm) {
|
2008-08-21 03:41:07 +02:00
|
|
|
unsigned ResultReg = createResultReg(RC);
|
2011-06-28 21:10:37 +02:00
|
|
|
const MCInstrDesc &II = TII.get(MachineInstOpcode);
|
2008-08-21 03:41:07 +02:00
|
|
|
|
2008-09-08 10:38:20 +02:00
|
|
|
if (II.getNumDefs() >= 1)
|
2014-02-18 23:05:46 +01:00
|
|
|
BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
|
2010-05-12 01:54:07 +02:00
|
|
|
.addReg(Op0, Op0IsKill * RegState::Kill)
|
|
|
|
.addImm(Imm);
|
2008-09-08 10:38:20 +02:00
|
|
|
else {
|
2014-02-18 23:05:46 +01:00
|
|
|
BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
|
2010-05-12 01:54:07 +02:00
|
|
|
.addReg(Op0, Op0IsKill * RegState::Kill)
|
|
|
|
.addImm(Imm);
|
2014-02-18 23:05:46 +01:00
|
|
|
BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
|
|
|
|
TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]);
|
2008-09-08 10:38:20 +02:00
|
|
|
}
|
2008-08-21 03:41:07 +02:00
|
|
|
return ResultReg;
|
|
|
|
}
|
|
|
|
|
2011-03-11 22:33:55 +01:00
|
|
|
unsigned FastISel::FastEmitInst_rii(unsigned MachineInstOpcode,
|
|
|
|
const TargetRegisterClass *RC,
|
|
|
|
unsigned Op0, bool Op0IsKill,
|
|
|
|
uint64_t Imm1, uint64_t Imm2) {
|
|
|
|
unsigned ResultReg = createResultReg(RC);
|
2011-06-28 21:10:37 +02:00
|
|
|
const MCInstrDesc &II = TII.get(MachineInstOpcode);
|
2011-03-11 22:33:55 +01:00
|
|
|
|
|
|
|
if (II.getNumDefs() >= 1)
|
2014-02-18 23:05:46 +01:00
|
|
|
BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
|
2011-03-11 22:33:55 +01:00
|
|
|
.addReg(Op0, Op0IsKill * RegState::Kill)
|
|
|
|
.addImm(Imm1)
|
|
|
|
.addImm(Imm2);
|
|
|
|
else {
|
2014-02-18 23:05:46 +01:00
|
|
|
BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
|
2011-03-11 22:33:55 +01:00
|
|
|
.addReg(Op0, Op0IsKill * RegState::Kill)
|
|
|
|
.addImm(Imm1)
|
|
|
|
.addImm(Imm2);
|
2014-02-18 23:05:46 +01:00
|
|
|
BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
|
|
|
|
TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]);
|
2011-03-11 22:33:55 +01:00
|
|
|
}
|
|
|
|
return ResultReg;
|
|
|
|
}
|
|
|
|
|
2008-08-27 03:09:54 +02:00
|
|
|
unsigned FastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
|
|
|
|
const TargetRegisterClass *RC,
|
2010-05-12 01:54:07 +02:00
|
|
|
unsigned Op0, bool Op0IsKill,
|
|
|
|
const ConstantFP *FPImm) {
|
2008-08-27 03:09:54 +02:00
|
|
|
unsigned ResultReg = createResultReg(RC);
|
2011-06-28 21:10:37 +02:00
|
|
|
const MCInstrDesc &II = TII.get(MachineInstOpcode);
|
2008-08-27 03:09:54 +02:00
|
|
|
|
2008-09-08 10:38:20 +02:00
|
|
|
if (II.getNumDefs() >= 1)
|
2014-02-18 23:05:46 +01:00
|
|
|
BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
|
2010-05-12 01:54:07 +02:00
|
|
|
.addReg(Op0, Op0IsKill * RegState::Kill)
|
|
|
|
.addFPImm(FPImm);
|
2008-09-08 10:38:20 +02:00
|
|
|
else {
|
2014-02-18 23:05:46 +01:00
|
|
|
BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
|
2010-05-12 01:54:07 +02:00
|
|
|
.addReg(Op0, Op0IsKill * RegState::Kill)
|
|
|
|
.addFPImm(FPImm);
|
2014-02-18 23:05:46 +01:00
|
|
|
BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
|
|
|
|
TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]);
|
2008-09-08 10:38:20 +02:00
|
|
|
}
|
2008-08-27 03:09:54 +02:00
|
|
|
return ResultReg;
|
|
|
|
}
|
|
|
|
|
2008-08-21 03:41:07 +02:00
|
|
|
unsigned FastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
|
|
|
|
const TargetRegisterClass *RC,
|
2010-05-12 01:54:07 +02:00
|
|
|
unsigned Op0, bool Op0IsKill,
|
|
|
|
unsigned Op1, bool Op1IsKill,
|
|
|
|
uint64_t Imm) {
|
2008-08-21 03:41:07 +02:00
|
|
|
unsigned ResultReg = createResultReg(RC);
|
2011-06-28 21:10:37 +02:00
|
|
|
const MCInstrDesc &II = TII.get(MachineInstOpcode);
|
2008-08-21 03:41:07 +02:00
|
|
|
|
2008-09-08 10:38:20 +02:00
|
|
|
if (II.getNumDefs() >= 1)
|
2014-02-18 23:05:46 +01:00
|
|
|
BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
|
2010-05-12 01:54:07 +02:00
|
|
|
.addReg(Op0, Op0IsKill * RegState::Kill)
|
|
|
|
.addReg(Op1, Op1IsKill * RegState::Kill)
|
|
|
|
.addImm(Imm);
|
2008-09-08 10:38:20 +02:00
|
|
|
else {
|
2014-02-18 23:05:46 +01:00
|
|
|
BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
|
2010-05-12 01:54:07 +02:00
|
|
|
.addReg(Op0, Op0IsKill * RegState::Kill)
|
|
|
|
.addReg(Op1, Op1IsKill * RegState::Kill)
|
|
|
|
.addImm(Imm);
|
2014-02-18 23:05:46 +01:00
|
|
|
BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
|
|
|
|
TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]);
|
2008-09-08 10:38:20 +02:00
|
|
|
}
|
2008-08-21 03:41:07 +02:00
|
|
|
return ResultReg;
|
|
|
|
}
|
2008-08-25 22:20:32 +02:00
|
|
|
|
2012-06-01 21:33:18 +02:00
|
|
|
unsigned FastISel::FastEmitInst_rrii(unsigned MachineInstOpcode,
|
|
|
|
const TargetRegisterClass *RC,
|
|
|
|
unsigned Op0, bool Op0IsKill,
|
|
|
|
unsigned Op1, bool Op1IsKill,
|
|
|
|
uint64_t Imm1, uint64_t Imm2) {
|
|
|
|
unsigned ResultReg = createResultReg(RC);
|
|
|
|
const MCInstrDesc &II = TII.get(MachineInstOpcode);
|
|
|
|
|
|
|
|
if (II.getNumDefs() >= 1)
|
2014-02-18 23:05:46 +01:00
|
|
|
BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
|
2012-06-01 21:33:18 +02:00
|
|
|
.addReg(Op0, Op0IsKill * RegState::Kill)
|
|
|
|
.addReg(Op1, Op1IsKill * RegState::Kill)
|
|
|
|
.addImm(Imm1).addImm(Imm2);
|
|
|
|
else {
|
2014-02-18 23:05:46 +01:00
|
|
|
BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
|
2012-06-01 21:33:18 +02:00
|
|
|
.addReg(Op0, Op0IsKill * RegState::Kill)
|
|
|
|
.addReg(Op1, Op1IsKill * RegState::Kill)
|
|
|
|
.addImm(Imm1).addImm(Imm2);
|
2014-02-18 23:05:46 +01:00
|
|
|
BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
|
|
|
|
TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]);
|
2012-06-01 21:33:18 +02:00
|
|
|
}
|
|
|
|
return ResultReg;
|
|
|
|
}
|
|
|
|
|
2008-08-25 22:20:32 +02:00
|
|
|
unsigned FastISel::FastEmitInst_i(unsigned MachineInstOpcode,
|
|
|
|
const TargetRegisterClass *RC,
|
|
|
|
uint64_t Imm) {
|
|
|
|
unsigned ResultReg = createResultReg(RC);
|
2011-06-28 21:10:37 +02:00
|
|
|
const MCInstrDesc &II = TII.get(MachineInstOpcode);
|
2010-11-23 04:31:01 +01:00
|
|
|
|
2008-09-08 10:38:20 +02:00
|
|
|
if (II.getNumDefs() >= 1)
|
2014-02-18 23:05:46 +01:00
|
|
|
BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg).addImm(Imm);
|
2008-09-08 10:38:20 +02:00
|
|
|
else {
|
2014-02-18 23:05:46 +01:00
|
|
|
BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II).addImm(Imm);
|
|
|
|
BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
|
|
|
|
TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]);
|
2008-09-08 10:38:20 +02:00
|
|
|
}
|
2008-08-25 22:20:32 +02:00
|
|
|
return ResultReg;
|
2008-08-26 00:20:39 +02:00
|
|
|
}
|
2008-08-28 00:30:02 +02:00
|
|
|
|
2011-04-23 01:38:06 +02:00
|
|
|
unsigned FastISel::FastEmitInst_ii(unsigned MachineInstOpcode,
|
|
|
|
const TargetRegisterClass *RC,
|
|
|
|
uint64_t Imm1, uint64_t Imm2) {
|
|
|
|
unsigned ResultReg = createResultReg(RC);
|
2011-06-28 21:10:37 +02:00
|
|
|
const MCInstrDesc &II = TII.get(MachineInstOpcode);
|
2011-04-23 01:38:06 +02:00
|
|
|
|
|
|
|
if (II.getNumDefs() >= 1)
|
2014-02-18 23:05:46 +01:00
|
|
|
BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
|
2011-04-23 01:38:06 +02:00
|
|
|
.addImm(Imm1).addImm(Imm2);
|
|
|
|
else {
|
2014-02-18 23:05:46 +01:00
|
|
|
BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II).addImm(Imm1).addImm(Imm2);
|
|
|
|
BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
|
|
|
|
TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]);
|
2011-04-23 01:38:06 +02:00
|
|
|
}
|
|
|
|
return ResultReg;
|
|
|
|
}
|
|
|
|
|
2009-08-11 22:47:22 +02:00
|
|
|
unsigned FastISel::FastEmitInst_extractsubreg(MVT RetVT,
|
2010-05-12 01:54:07 +02:00
|
|
|
unsigned Op0, bool Op0IsKill,
|
|
|
|
uint32_t Idx) {
|
2009-01-22 10:10:11 +01:00
|
|
|
unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
|
2010-07-08 18:40:22 +02:00
|
|
|
assert(TargetRegisterInfo::isVirtualRegister(Op0) &&
|
|
|
|
"Cannot yet extract from physregs");
|
2012-05-20 08:38:37 +02:00
|
|
|
const TargetRegisterClass *RC = MRI.getRegClass(Op0);
|
|
|
|
MRI.constrainRegClass(Op0, TRI.getSubClassWithSubReg(RC, Idx));
|
2010-07-10 11:00:22 +02:00
|
|
|
BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
|
2014-02-18 23:05:46 +01:00
|
|
|
DbgLoc, TII.get(TargetOpcode::COPY), ResultReg)
|
2010-07-08 18:40:22 +02:00
|
|
|
.addReg(Op0, getKillRegState(Op0IsKill), Idx);
|
2008-08-28 00:30:02 +02:00
|
|
|
return ResultReg;
|
|
|
|
}
|
2009-03-13 21:42:20 +01:00
|
|
|
|
|
|
|
/// FastEmitZExtFromI1 - Emit MachineInstrs to compute the value of Op
|
|
|
|
/// with all but the least significant bit set to zero.
|
2010-05-12 01:54:07 +02:00
|
|
|
unsigned FastISel::FastEmitZExtFromI1(MVT VT, unsigned Op0, bool Op0IsKill) {
|
|
|
|
return FastEmit_ri(VT, VT, ISD::AND, Op0, Op0IsKill, 1);
|
2009-03-13 21:42:20 +01:00
|
|
|
}
|
2010-04-22 22:46:50 +02:00
|
|
|
|
|
|
|
/// HandlePHINodesInSuccessorBlocks - Handle PHI nodes in successor blocks.
|
|
|
|
/// Emit code to ensure constants are copied into registers when needed.
|
|
|
|
/// Remember the virtual registers that need to be added to the Machine PHI
|
|
|
|
/// nodes as input. We cannot just directly add them, because expansion
|
|
|
|
/// might result in multiple MBB's for one BB. As such, the start of the
|
|
|
|
/// BB might correspond to a different MBB than the end.
|
|
|
|
bool FastISel::HandlePHINodesInSuccessorBlocks(const BasicBlock *LLVMBB) {
|
|
|
|
const TerminatorInst *TI = LLVMBB->getTerminator();
|
|
|
|
|
|
|
|
SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
|
2010-07-07 18:29:44 +02:00
|
|
|
unsigned OrigNumPHINodesToUpdate = FuncInfo.PHINodesToUpdate.size();
|
2010-04-22 22:46:50 +02:00
|
|
|
|
|
|
|
// Check successor nodes' PHI nodes that expect a constant to be available
|
|
|
|
// from this block.
|
|
|
|
for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
|
|
|
|
const BasicBlock *SuccBB = TI->getSuccessor(succ);
|
|
|
|
if (!isa<PHINode>(SuccBB->begin())) continue;
|
2010-07-07 18:29:44 +02:00
|
|
|
MachineBasicBlock *SuccMBB = FuncInfo.MBBMap[SuccBB];
|
2010-04-22 22:46:50 +02:00
|
|
|
|
|
|
|
// If this terminator has multiple identical successors (common for
|
|
|
|
// switches), only handle each succ once.
|
|
|
|
if (!SuccsHandled.insert(SuccMBB)) continue;
|
|
|
|
|
|
|
|
MachineBasicBlock::iterator MBBI = SuccMBB->begin();
|
|
|
|
|
|
|
|
// At this point we know that there is a 1-1 correspondence between LLVM PHI
|
|
|
|
// nodes and Machine PHI nodes, but the incoming operands have not been
|
|
|
|
// emitted yet.
|
|
|
|
for (BasicBlock::const_iterator I = SuccBB->begin();
|
|
|
|
const PHINode *PN = dyn_cast<PHINode>(I); ++I) {
|
2010-05-07 03:10:20 +02:00
|
|
|
|
2010-04-22 22:46:50 +02:00
|
|
|
// Ignore dead phi's.
|
|
|
|
if (PN->use_empty()) continue;
|
|
|
|
|
|
|
|
// Only handle legal types. Two interesting things to note here. First,
|
|
|
|
// by bailing out early, we may leave behind some dead instructions,
|
|
|
|
// since SelectionDAG's HandlePHINodesInSuccessorBlocks will insert its
|
2011-04-15 07:18:47 +02:00
|
|
|
// own moves. Second, this check is necessary because FastISel doesn't
|
2010-07-02 02:10:16 +02:00
|
|
|
// use CreateRegs to create registers, so it always creates
|
2010-04-22 22:46:50 +02:00
|
|
|
// exactly one register for each non-void instruction.
|
|
|
|
EVT VT = TLI.getValueType(PN->getType(), /*AllowUnknown=*/true);
|
|
|
|
if (VT == MVT::Other || !TLI.isTypeLegal(VT)) {
|
2012-02-04 01:39:19 +01:00
|
|
|
// Handle integer promotions, though, because they're common and easy.
|
|
|
|
if (VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16)
|
2010-04-22 22:46:50 +02:00
|
|
|
VT = TLI.getTypeToTransformTo(LLVMBB->getContext(), VT);
|
|
|
|
else {
|
2010-07-07 18:29:44 +02:00
|
|
|
FuncInfo.PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
|
2010-04-22 22:46:50 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
|
|
|
|
|
2010-05-07 03:10:20 +02:00
|
|
|
// Set the DebugLoc for the copy. Prefer the location of the operand
|
|
|
|
// if there is one; use the location of the PHI otherwise.
|
2014-02-18 23:05:46 +01:00
|
|
|
DbgLoc = PN->getDebugLoc();
|
2010-05-07 03:10:20 +02:00
|
|
|
if (const Instruction *Inst = dyn_cast<Instruction>(PHIOp))
|
2014-02-18 23:05:46 +01:00
|
|
|
DbgLoc = Inst->getDebugLoc();
|
2010-05-07 03:10:20 +02:00
|
|
|
|
2010-04-22 22:46:50 +02:00
|
|
|
unsigned Reg = getRegForValue(PHIOp);
|
|
|
|
if (Reg == 0) {
|
2010-07-07 18:29:44 +02:00
|
|
|
FuncInfo.PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
|
2010-04-22 22:46:50 +02:00
|
|
|
return false;
|
|
|
|
}
|
2010-07-07 18:29:44 +02:00
|
|
|
FuncInfo.PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg));
|
2014-02-18 23:05:46 +01:00
|
|
|
DbgLoc = DebugLoc();
|
2010-04-22 22:46:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2013-04-20 00:29:18 +02:00
|
|
|
|
|
|
|
bool FastISel::tryToFoldLoad(const LoadInst *LI, const Instruction *FoldInst) {
|
2013-04-20 01:26:18 +02:00
|
|
|
assert(LI->hasOneUse() &&
|
|
|
|
"tryToFoldLoad expected a LoadInst with a single use");
|
2013-04-20 00:29:18 +02:00
|
|
|
// We know that the load has a single use, but don't know what it is. If it
|
|
|
|
// isn't one of the folded instructions, then we can't succeed here. Handle
|
|
|
|
// this by scanning the single-use users of the load until we get to FoldInst.
|
|
|
|
unsigned MaxUsers = 6; // Don't scan down huge single-use chains of instrs.
|
|
|
|
|
2014-03-09 04:16:01 +01:00
|
|
|
const Instruction *TheUser = LI->user_back();
|
2013-04-20 00:29:18 +02:00
|
|
|
while (TheUser != FoldInst && // Scan up until we find FoldInst.
|
|
|
|
// Stay in the right block.
|
|
|
|
TheUser->getParent() == FoldInst->getParent() &&
|
|
|
|
--MaxUsers) { // Don't scan too far.
|
|
|
|
// If there are multiple or no uses of this instruction, then bail out.
|
|
|
|
if (!TheUser->hasOneUse())
|
|
|
|
return false;
|
|
|
|
|
2014-03-09 04:16:01 +01:00
|
|
|
TheUser = TheUser->user_back();
|
2013-04-20 00:29:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// If we didn't find the fold instruction, then we failed to collapse the
|
|
|
|
// sequence.
|
|
|
|
if (TheUser != FoldInst)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Don't try to fold volatile loads. Target has to deal with alignment
|
|
|
|
// constraints.
|
2013-04-20 01:26:18 +02:00
|
|
|
if (LI->isVolatile())
|
|
|
|
return false;
|
2013-04-20 00:29:18 +02:00
|
|
|
|
|
|
|
// Figure out which vreg this is going into. If there is no assigned vreg yet
|
|
|
|
// then there actually was no reference to it. Perhaps the load is referenced
|
|
|
|
// by a dead instruction.
|
|
|
|
unsigned LoadReg = getRegForValue(LI);
|
|
|
|
if (LoadReg == 0)
|
|
|
|
return false;
|
|
|
|
|
2013-04-20 01:26:18 +02:00
|
|
|
// We can't fold if this vreg has no uses or more than one use. Multiple uses
|
|
|
|
// may mean that the instruction got lowered to multiple MIs, or the use of
|
|
|
|
// the loaded value ended up being multiple operands of the result.
|
|
|
|
if (!MRI.hasOneUse(LoadReg))
|
2013-04-20 00:29:18 +02:00
|
|
|
return false;
|
|
|
|
|
2013-04-20 01:26:18 +02:00
|
|
|
MachineRegisterInfo::reg_iterator RI = MRI.reg_begin(LoadReg);
|
2014-03-14 00:12:04 +01:00
|
|
|
MachineInstr *User = RI->getParent();
|
2013-04-20 00:29:18 +02:00
|
|
|
|
|
|
|
// Set the insertion point properly. Folding the load can cause generation of
|
2013-04-20 01:26:18 +02:00
|
|
|
// other random instructions (like sign extends) for addressing modes; make
|
2013-04-20 00:29:18 +02:00
|
|
|
// sure they get inserted in a logical place before the new instruction.
|
|
|
|
FuncInfo.InsertPt = User;
|
|
|
|
FuncInfo.MBB = User->getParent();
|
|
|
|
|
|
|
|
// Ask the target to try folding the load.
|
|
|
|
return tryToFoldLoadIntoMI(User, RI.getOperandNo(), LI);
|
|
|
|
}
|
|
|
|
|
2013-11-15 20:09:27 +01:00
|
|
|
bool FastISel::canFoldAddIntoGEP(const User *GEP, const Value *Add) {
|
|
|
|
// Must be an add.
|
|
|
|
if (!isa<AddOperator>(Add))
|
|
|
|
return false;
|
|
|
|
// Type size needs to match.
|
2014-02-18 23:05:46 +01:00
|
|
|
if (DL.getTypeSizeInBits(GEP->getType()) !=
|
|
|
|
DL.getTypeSizeInBits(Add->getType()))
|
2013-11-15 20:09:27 +01:00
|
|
|
return false;
|
|
|
|
// Must be in the same basic block.
|
|
|
|
if (isa<Instruction>(Add) &&
|
|
|
|
FuncInfo.MBBMap[cast<Instruction>(Add)->getParent()] != FuncInfo.MBB)
|
|
|
|
return false;
|
|
|
|
// Must have a constant operand.
|
|
|
|
return isa<ConstantInt>(cast<AddOperator>(Add)->getOperand(1));
|
|
|
|
}
|
2013-04-20 00:29:18 +02:00
|
|
|
|