2010-04-22 20:06:42 +00:00
|
|
|
//===-- FastISel.cpp - Implementation of the FastISel class ---------------===//
|
2008-08-13 20:19:35 +00: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 20:48:29 +00: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 01:59:13 +00: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 20:48:29 +00: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 01:59:13 +00:00
|
|
|
// support. In many cases, this allows us to avoid duplicating a lot of
|
2008-09-30 20:48:29 +00: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 01:59:13 +00:00
|
|
|
// weighed against the speed at which the code can be generated. Also,
|
2008-09-30 20:48:29 +00: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 01:59:13 +00:00
|
|
|
// time. Despite its limitations, "fast" instruction selection is able to
|
2008-09-30 20:48:29 +00: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 01:59:13 +00:00
|
|
|
// from simple operators. More complicated operations currently require
|
2008-09-30 20:48:29 +00:00
|
|
|
// target-specific code.
|
|
|
|
//
|
2008-08-13 20:19:35 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2011-11-16 21:05:28 +00:00
|
|
|
#define DEBUG_TYPE "isel"
|
2008-09-25 17:05:24 +00:00
|
|
|
#include "llvm/Function.h"
|
|
|
|
#include "llvm/GlobalVariable.h"
|
2008-08-19 22:31:46 +00:00
|
|
|
#include "llvm/Instructions.h"
|
2008-09-25 17:05:24 +00:00
|
|
|
#include "llvm/IntrinsicInst.h"
|
2011-04-11 09:35:34 +00:00
|
|
|
#include "llvm/Operator.h"
|
2011-05-16 20:27:46 +00:00
|
|
|
#include "llvm/CodeGen/Analysis.h"
|
2008-08-13 20:19:35 +00:00
|
|
|
#include "llvm/CodeGen/FastISel.h"
|
2010-07-07 16:01:37 +00:00
|
|
|
#include "llvm/CodeGen/FunctionLoweringInfo.h"
|
2008-08-13 20:19:35 +00:00
|
|
|
#include "llvm/CodeGen/MachineInstrBuilder.h"
|
2008-09-25 17:05:24 +00:00
|
|
|
#include "llvm/CodeGen/MachineModuleInfo.h"
|
2008-08-13 20:19:35 +00:00
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
2009-01-13 00:35:13 +00:00
|
|
|
#include "llvm/Analysis/DebugInfo.h"
|
2010-07-01 03:49:38 +00:00
|
|
|
#include "llvm/Analysis/Loads.h"
|
2008-08-20 22:45:34 +00:00
|
|
|
#include "llvm/Target/TargetData.h"
|
2008-08-13 20:19:35 +00:00
|
|
|
#include "llvm/Target/TargetInstrInfo.h"
|
2008-08-20 22:45:34 +00:00
|
|
|
#include "llvm/Target/TargetLowering.h"
|
2008-08-20 21:05:57 +00:00
|
|
|
#include "llvm/Target/TargetMachine.h"
|
2010-04-20 15:00:41 +00:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2010-12-06 22:39:26 +00:00
|
|
|
#include "llvm/Support/Debug.h"
|
2011-11-16 21:05:28 +00:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
2008-08-13 20:19:35 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
2011-11-28 19:59:09 +00:00
|
|
|
STATISTIC(NumFastIselSuccessIndependent, "Number of insts selected by "
|
|
|
|
"target-independent selector");
|
|
|
|
STATISTIC(NumFastIselSuccessTarget, "Number of insts selected by "
|
|
|
|
"target-specific selector");
|
2011-11-29 19:40:47 +00:00
|
|
|
STATISTIC(NumFastIselDead, "Number of dead insts removed on failure");
|
2011-11-16 21:05:28 +00:00
|
|
|
|
2010-07-10 09:00:22 +00: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();
|
|
|
|
|
2011-08-18 22:06:10 +00:00
|
|
|
EmitStartPt = 0;
|
2010-07-10 09:00:22 +00:00
|
|
|
|
2011-08-18 22:06:10 +00:00
|
|
|
// Advance the emit start point past any EH_LABEL instructions.
|
2010-07-10 09:00:22 +00:00
|
|
|
MachineBasicBlock::iterator
|
|
|
|
I = FuncInfo.MBB->begin(), E = FuncInfo.MBB->end();
|
|
|
|
while (I != E && I->getOpcode() == TargetOpcode::EH_LABEL) {
|
2011-08-18 22:06:10 +00:00
|
|
|
EmitStartPt = I;
|
2010-07-10 09:00:22 +00:00
|
|
|
++I;
|
|
|
|
}
|
2011-08-18 22:06:10 +00:00
|
|
|
LastLocalValue = EmitStartPt;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FastISel::flushLocalValueMap() {
|
|
|
|
LocalValueMap.clear();
|
|
|
|
LastLocalValue = EmitStartPt;
|
|
|
|
recomputeInsertPt();
|
2010-07-10 09:00:22 +00:00
|
|
|
}
|
|
|
|
|
2010-05-11 23:54:07 +00:00
|
|
|
bool FastISel::hasTrivialKill(const Value *V) const {
|
2010-05-14 22:53:18 +00:00
|
|
|
// Don't consider constants or arguments to have trivial kills.
|
2010-05-11 23:54:07 +00:00
|
|
|
const Instruction *I = dyn_cast<Instruction>(V);
|
2010-05-14 22:53:18 +00:00
|
|
|
if (!I)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// No-op casts are trivially coalesced by fast-isel.
|
|
|
|
if (const CastInst *Cast = dyn_cast<CastInst>(I))
|
|
|
|
if (Cast->isNoopCast(TD.getIntPtrType(Cast->getContext())) &&
|
|
|
|
!hasTrivialKill(Cast->getOperand(0)))
|
|
|
|
return false;
|
|
|
|
|
2011-11-15 23:34:05 +00: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-14 22:53:18 +00: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) &&
|
2010-07-22 13:36:47 +00:00
|
|
|
cast<Instruction>(*I->use_begin())->getParent() == I->getParent();
|
2010-05-11 23:54:07 +00:00
|
|
|
}
|
|
|
|
|
2010-04-15 01:51:59 +00:00
|
|
|
unsigned FastISel::getRegForValue(const Value *V) {
|
2009-08-10 22:56:29 +00:00
|
|
|
EVT RealVT = TLI.getValueType(V->getType(), /*AllowUnknown=*/true);
|
2009-04-07 20:40:11 +00:00
|
|
|
// Don't handle non-simple values in FastISel.
|
|
|
|
if (!RealVT.isSimple())
|
|
|
|
return 0;
|
2008-09-10 21:01:08 +00:00
|
|
|
|
2008-12-08 07:57:47 +00: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 20:47:22 +00:00
|
|
|
MVT VT = RealVT.getSimpleVT();
|
2008-09-10 21:01:08 +00:00
|
|
|
if (!TLI.isTypeLegal(VT)) {
|
2011-05-25 23:49:02 +00:00
|
|
|
// Handle integer promotions, though, because they're common and easy.
|
|
|
|
if (VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16)
|
2009-08-12 00:36:31 +00:00
|
|
|
VT = TLI.getTypeToTransformTo(V->getContext(), VT).getSimpleVT();
|
2008-09-10 21:01:08 +00:00
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-12-08 07:57:47 +00: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-01-12 04:30:26 +00:00
|
|
|
// def-dominates-use requirement enforced.
|
2010-07-07 16:29:44 +00:00
|
|
|
DenseMap<const Value *, unsigned>::iterator I = FuncInfo.ValueMap.find(V);
|
2011-04-17 01:16:47 +00:00
|
|
|
if (I != FuncInfo.ValueMap.end())
|
|
|
|
return I->second;
|
|
|
|
|
2008-12-08 07:57:47 +00:00
|
|
|
unsigned Reg = LocalValueMap[V];
|
|
|
|
if (Reg != 0)
|
|
|
|
return Reg;
|
|
|
|
|
2010-05-06 00:02:14 +00: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 09:00:22 +00:00
|
|
|
if (isa<Instruction>(V) &&
|
|
|
|
(!isa<AllocaInst>(V) ||
|
|
|
|
!FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(V))))
|
|
|
|
return FuncInfo.InitializeRegForValue(V);
|
|
|
|
|
2010-07-14 01:07:44 +00:00
|
|
|
SavePoint SaveInsertPt = enterLocalValueArea();
|
2010-07-10 09:00:22 +00:00
|
|
|
|
|
|
|
// Materialize the value in a register. Emit any instructions in the
|
|
|
|
// local value area.
|
|
|
|
Reg = materializeRegForValue(V, VT);
|
|
|
|
|
|
|
|
leaveLocalValueArea(SaveInsertPt);
|
2010-05-06 00:02:14 +00:00
|
|
|
|
2010-07-10 09:00:22 +00:00
|
|
|
return Reg;
|
2010-05-03 23:36:34 +00:00
|
|
|
}
|
|
|
|
|
2010-08-17 01:30:33 +00:00
|
|
|
/// materializeRegForValue - Helper for getRegForValue. This function is
|
2010-05-03 23:36:34 +00: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 01:51:59 +00:00
|
|
|
if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
|
2008-09-19 22:16:54 +00:00
|
|
|
if (CI->getValue().getActiveBits() <= 64)
|
|
|
|
Reg = FastEmit_i(VT, VT, ISD::Constant, CI->getZExtValue());
|
2008-09-10 20:11:02 +00:00
|
|
|
} else if (isa<AllocaInst>(V)) {
|
2008-09-19 22:16:54 +00:00
|
|
|
Reg = TargetMaterializeAlloca(cast<AllocaInst>(V));
|
2008-08-28 21:19:07 +00:00
|
|
|
} else if (isa<ConstantPointerNull>(V)) {
|
2008-10-07 22:03:27 +00:00
|
|
|
// Translate this as an integer zero so that it can be
|
|
|
|
// local-CSE'd with actual integer zeros.
|
2009-08-13 21:58:54 +00:00
|
|
|
Reg =
|
|
|
|
getRegForValue(Constant::getNullValue(TD.getIntPtrType(V->getContext())));
|
2010-04-15 01:51:59 +00:00
|
|
|
} else if (const ConstantFP *CF = dyn_cast<ConstantFP>(V)) {
|
2011-04-28 00:42:03 +00:00
|
|
|
if (CF->isNullValue()) {
|
2011-04-27 22:41:55 +00:00
|
|
|
Reg = TargetMaterializeFloatZero(CF);
|
|
|
|
} else {
|
|
|
|
// Try to emit the constant directly.
|
|
|
|
Reg = FastEmit_f(VT, VT, ISD::ConstantFP, CF);
|
|
|
|
}
|
2008-08-27 18:10:19 +00:00
|
|
|
|
|
|
|
if (!Reg) {
|
2010-04-13 17:07:06 +00:00
|
|
|
// Try to emit the constant by using an integer constant with a cast.
|
2008-08-27 18:10:19 +00:00
|
|
|
const APFloat &Flt = CF->getValueAPF();
|
2009-08-10 22:56:29 +00:00
|
|
|
EVT IntVT = TLI.getPointerTy();
|
2008-08-27 18:10:19 +00:00
|
|
|
|
|
|
|
uint64_t x[2];
|
|
|
|
uint32_t IntBitWidth = IntVT.getSizeInBits();
|
2008-10-09 23:00:39 +00:00
|
|
|
bool isExact;
|
|
|
|
(void) Flt.convertToInteger(x, IntBitWidth, /*isSigned=*/true,
|
|
|
|
APFloat::rmTowardZero, &isExact);
|
|
|
|
if (isExact) {
|
2011-07-18 21:45:40 +00:00
|
|
|
APInt IntVal(IntBitWidth, x);
|
2008-08-27 18:10:19 +00:00
|
|
|
|
2009-07-22 00:24:57 +00:00
|
|
|
unsigned IntegerReg =
|
2009-07-24 23:12:02 +00:00
|
|
|
getRegForValue(ConstantInt::get(V->getContext(), IntVal));
|
2008-09-19 22:16:54 +00:00
|
|
|
if (IntegerReg != 0)
|
2010-05-11 23:54:07 +00:00
|
|
|
Reg = FastEmit_r(IntVT.getSimpleVT(), VT, ISD::SINT_TO_FP,
|
|
|
|
IntegerReg, /*Kill=*/false);
|
2008-09-19 22:16:54 +00:00
|
|
|
}
|
2008-08-27 18:10:19 +00:00
|
|
|
}
|
2010-04-15 01:51:59 +00:00
|
|
|
} else if (const Operator *Op = dyn_cast<Operator>(V)) {
|
2010-07-01 02:58:57 +00:00
|
|
|
if (!SelectOperator(Op, Op->getOpcode()))
|
|
|
|
if (!isa<Instruction>(Op) ||
|
|
|
|
!TargetSelectInstruction(cast<Instruction>(Op)))
|
|
|
|
return 0;
|
2010-06-21 14:17:46 +00:00
|
|
|
Reg = lookUpRegForValue(Op);
|
2008-08-28 21:19:07 +00:00
|
|
|
} else if (isa<UndefValue>(V)) {
|
2008-09-03 23:32:19 +00:00
|
|
|
Reg = createResultReg(TLI.getRegClassFor(VT));
|
2010-07-10 09:00:22 +00:00
|
|
|
BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
|
|
|
|
TII.get(TargetOpcode::IMPLICIT_DEF), Reg);
|
2008-08-27 18:10:19 +00:00
|
|
|
}
|
2010-11-23 03:31:01 +00:00
|
|
|
|
2008-09-25 01:28:51 +00:00
|
|
|
// If target-independent code couldn't handle the value, give target-specific
|
|
|
|
// code a try.
|
2008-09-05 23:36:01 +00:00
|
|
|
if (!Reg && isa<Constant>(V))
|
2008-09-19 22:16:54 +00:00
|
|
|
Reg = TargetMaterializeConstant(cast<Constant>(V));
|
2010-11-23 03:31:01 +00:00
|
|
|
|
2008-09-19 22:16:54 +00:00
|
|
|
// Don't cache constant materializations in the general ValueMap.
|
|
|
|
// To do so would require tracking what uses they dominate.
|
2010-07-10 09:00:22 +00:00
|
|
|
if (Reg != 0) {
|
2008-09-25 01:28:51 +00:00
|
|
|
LocalValueMap[V] = Reg;
|
2010-07-10 09:00:22 +00:00
|
|
|
LastLocalValue = MRI.getVRegDef(Reg);
|
|
|
|
}
|
2008-09-03 23:32:19 +00:00
|
|
|
return Reg;
|
2008-08-27 18:10:19 +00:00
|
|
|
}
|
|
|
|
|
2010-04-15 01:51:59 +00:00
|
|
|
unsigned FastISel::lookUpRegForValue(const Value *V) {
|
2008-09-09 01:26:59 +00: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-03 23:36:34 +00:00
|
|
|
// def-dominates-use requirement enforced.
|
2010-07-07 16:29:44 +00:00
|
|
|
DenseMap<const Value *, unsigned>::iterator I = FuncInfo.ValueMap.find(V);
|
|
|
|
if (I != FuncInfo.ValueMap.end())
|
2010-06-21 14:21:47 +00:00
|
|
|
return I->second;
|
2008-09-09 01:26:59 +00:00
|
|
|
return LocalValueMap[V];
|
|
|
|
}
|
|
|
|
|
2008-08-30 00:38:46 +00: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 21:06:17 +00:00
|
|
|
void FastISel::UpdateValueMap(const Value *I, unsigned Reg, unsigned NumRegs) {
|
2008-09-05 18:18:20 +00:00
|
|
|
if (!isa<Instruction>(I)) {
|
|
|
|
LocalValueMap[I] = Reg;
|
2011-05-16 21:06:17 +00:00
|
|
|
return;
|
2009-04-12 07:45:01 +00:00
|
|
|
}
|
2010-11-23 03:31:01 +00:00
|
|
|
|
2010-07-07 16:29:44 +00:00
|
|
|
unsigned &AssignedReg = FuncInfo.ValueMap[I];
|
2009-04-12 07:45:01 +00:00
|
|
|
if (AssignedReg == 0)
|
2010-07-10 09:00:22 +00:00
|
|
|
// Use the new register.
|
2009-04-12 07:45:01 +00:00
|
|
|
AssignedReg = Reg;
|
2009-04-12 07:46:30 +00:00
|
|
|
else if (Reg != AssignedReg) {
|
2010-07-10 09:00:22 +00:00
|
|
|
// Arrange for uses of AssignedReg to be replaced by uses of Reg.
|
2011-05-16 21:06:17 +00:00
|
|
|
for (unsigned i = 0; i < NumRegs; i++)
|
|
|
|
FuncInfo.RegFixups[AssignedReg+i] = Reg+i;
|
2010-07-10 09:00:22 +00:00
|
|
|
|
|
|
|
AssignedReg = Reg;
|
2008-09-05 18:18:20 +00:00
|
|
|
}
|
2008-08-30 00:38:46 +00:00
|
|
|
}
|
|
|
|
|
2010-05-11 23:54:07 +00:00
|
|
|
std::pair<unsigned, bool> FastISel::getRegForGEPIndex(const Value *Idx) {
|
2008-12-08 07:57:47 +00:00
|
|
|
unsigned IdxN = getRegForValue(Idx);
|
|
|
|
if (IdxN == 0)
|
|
|
|
// Unhandled operand. Halt "fast" selection and bail.
|
2010-05-11 23:54:07 +00:00
|
|
|
return std::pair<unsigned, bool>(0, false);
|
|
|
|
|
|
|
|
bool IdxNIsKill = hasTrivialKill(Idx);
|
2008-12-08 07:57:47 +00:00
|
|
|
|
|
|
|
// If the index is smaller or larger than intptr_t, truncate or extend it.
|
2009-08-11 21:59:30 +00:00
|
|
|
MVT PtrVT = TLI.getPointerTy();
|
2009-08-10 22:56:29 +00:00
|
|
|
EVT IdxVT = EVT::getEVT(Idx->getType(), /*HandleUnknown=*/false);
|
2010-05-11 23:54:07 +00: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 07:57:47 +00:00
|
|
|
}
|
|
|
|
|
2010-07-10 09:00:22 +00:00
|
|
|
void FastISel::recomputeInsertPt() {
|
|
|
|
if (getLastLocalValue()) {
|
|
|
|
FuncInfo.InsertPt = getLastLocalValue();
|
2010-07-19 22:48:56 +00:00
|
|
|
FuncInfo.MBB = FuncInfo.InsertPt->getParent();
|
2010-07-10 09:00:22 +00: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 19:40:47 +00: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();
|
|
|
|
++NumFastIselDead;
|
|
|
|
}
|
|
|
|
recomputeInsertPt();
|
|
|
|
}
|
|
|
|
|
2010-07-14 01:07:44 +00:00
|
|
|
FastISel::SavePoint FastISel::enterLocalValueArea() {
|
2010-07-10 09:00:22 +00:00
|
|
|
MachineBasicBlock::iterator OldInsertPt = FuncInfo.InsertPt;
|
2010-07-14 22:01:31 +00:00
|
|
|
DebugLoc OldDL = DL;
|
2010-07-10 09:00:22 +00:00
|
|
|
recomputeInsertPt();
|
2010-07-14 01:07:44 +00:00
|
|
|
DL = DebugLoc();
|
2010-07-14 22:01:31 +00:00
|
|
|
SavePoint SP = { OldInsertPt, OldDL };
|
2010-07-14 01:07:44 +00:00
|
|
|
return SP;
|
2010-07-10 09:00:22 +00:00
|
|
|
}
|
|
|
|
|
2010-07-14 01:07:44 +00:00
|
|
|
void FastISel::leaveLocalValueArea(SavePoint OldInsertPt) {
|
2010-07-10 09:00:22 +00:00
|
|
|
if (FuncInfo.InsertPt != FuncInfo.MBB->begin())
|
|
|
|
LastLocalValue = llvm::prior(FuncInfo.InsertPt);
|
|
|
|
|
|
|
|
// Restore the previous insert position.
|
2010-07-14 01:07:44 +00:00
|
|
|
FuncInfo.InsertPt = OldInsertPt.InsertPt;
|
|
|
|
DL = OldInsertPt.DL;
|
2010-07-10 09:00:22 +00:00
|
|
|
}
|
|
|
|
|
2008-08-20 00:11:48 +00: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 01:51:59 +00:00
|
|
|
bool FastISel::SelectBinaryOp(const User *I, unsigned ISDOpcode) {
|
2009-08-10 22:56:29 +00:00
|
|
|
EVT VT = EVT::getEVT(I->getType(), /*HandleUnknown=*/true);
|
2009-08-11 20:47:22 +00:00
|
|
|
if (VT == MVT::Other || !VT.isSimple())
|
2008-08-21 01:41:07 +00:00
|
|
|
// Unhandled type. Halt "fast" selection and bail.
|
|
|
|
return false;
|
2008-09-05 18:44:22 +00:00
|
|
|
|
2008-08-26 20:52:40 +00: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 18:44:22 +00:00
|
|
|
if (!TLI.isTypeLegal(VT)) {
|
2009-08-11 20:47:22 +00:00
|
|
|
// MVT::i1 is special. Allow AND, OR, or XOR because they
|
2008-09-05 18:44:22 +00:00
|
|
|
// don't require additional zeroing, which makes them easy.
|
2009-08-11 20:47:22 +00:00
|
|
|
if (VT == MVT::i1 &&
|
2008-09-25 17:22:52 +00:00
|
|
|
(ISDOpcode == ISD::AND || ISDOpcode == ISD::OR ||
|
|
|
|
ISDOpcode == ISD::XOR))
|
2009-08-12 00:36:31 +00:00
|
|
|
VT = TLI.getTypeToTransformTo(I->getContext(), VT);
|
2008-09-05 18:44:22 +00:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
2008-08-21 01:41:07 +00:00
|
|
|
|
2011-04-17 01:16:47 +00: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-22 23:38:06 +00:00
|
|
|
|
2011-04-17 20:23:29 +00:00
|
|
|
unsigned ResultReg = FastEmit_ri_(VT.getSimpleVT(), ISDOpcode, Op1,
|
|
|
|
Op1IsKill, CI->getZExtValue(),
|
|
|
|
VT.getSimpleVT());
|
|
|
|
if (ResultReg == 0) return false;
|
2011-04-22 23:38:06 +00:00
|
|
|
|
2011-04-17 20:23:29 +00:00
|
|
|
// We successfully emitted code for the given LLVM Instruction.
|
|
|
|
UpdateValueMap(I, ResultReg);
|
|
|
|
return true;
|
2011-04-17 01:16:47 +00:00
|
|
|
}
|
2011-04-22 23:38:06 +00:00
|
|
|
|
|
|
|
|
2008-09-03 23:12:08 +00:00
|
|
|
unsigned Op0 = getRegForValue(I->getOperand(0));
|
2011-04-17 20:23:29 +00:00
|
|
|
if (Op0 == 0) // Unhandled operand. Halt "fast" selection and bail.
|
2008-08-20 00:35:17 +00:00
|
|
|
return false;
|
|
|
|
|
2010-05-11 23:54:07 +00:00
|
|
|
bool Op0IsKill = hasTrivialKill(I->getOperand(0));
|
|
|
|
|
2008-08-21 01:41:07 +00: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 20:23:29 +00:00
|
|
|
uint64_t Imm = CI->getZExtValue();
|
2011-04-22 23:38:06 +00:00
|
|
|
|
2011-04-18 07:00:40 +00: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-22 23:38:06 +00:00
|
|
|
|
2011-04-17 20:23:29 +00:00
|
|
|
unsigned ResultReg = FastEmit_ri_(VT.getSimpleVT(), ISDOpcode, Op0,
|
|
|
|
Op0IsKill, Imm, VT.getSimpleVT());
|
|
|
|
if (ResultReg == 0) return false;
|
2011-04-22 23:38:06 +00:00
|
|
|
|
2011-04-17 20:23:29 +00:00
|
|
|
// We successfully emitted code for the given LLVM Instruction.
|
|
|
|
UpdateValueMap(I, ResultReg);
|
|
|
|
return true;
|
2008-08-21 01:41:07 +00:00
|
|
|
}
|
|
|
|
|
2008-08-27 01:09:54 +00:00
|
|
|
// Check if the second operand is a constant float.
|
|
|
|
if (ConstantFP *CF = dyn_cast<ConstantFP>(I->getOperand(1))) {
|
2008-08-27 18:10:19 +00:00
|
|
|
unsigned ResultReg = FastEmit_rf(VT.getSimpleVT(), VT.getSimpleVT(),
|
2010-05-11 23:54:07 +00:00
|
|
|
ISDOpcode, Op0, Op0IsKill, CF);
|
2008-08-27 18:10:19 +00:00
|
|
|
if (ResultReg != 0) {
|
|
|
|
// We successfully emitted code for the given LLVM Instruction.
|
2008-09-03 23:12:08 +00:00
|
|
|
UpdateValueMap(I, ResultReg);
|
2008-08-27 18:10:19 +00:00
|
|
|
return true;
|
|
|
|
}
|
2008-08-27 01:09:54 +00:00
|
|
|
}
|
|
|
|
|
2008-09-03 23:12:08 +00:00
|
|
|
unsigned Op1 = getRegForValue(I->getOperand(1));
|
2008-08-21 01:41:07 +00:00
|
|
|
if (Op1 == 0)
|
|
|
|
// Unhandled operand. Halt "fast" selection and bail.
|
2008-08-20 00:11:48 +00:00
|
|
|
return false;
|
|
|
|
|
2010-05-11 23:54:07 +00:00
|
|
|
bool Op1IsKill = hasTrivialKill(I->getOperand(1));
|
|
|
|
|
2008-08-27 18:10:19 +00:00
|
|
|
// Now we have both operands in registers. Emit the instruction.
|
2008-08-25 23:58:18 +00:00
|
|
|
unsigned ResultReg = FastEmit_rr(VT.getSimpleVT(), VT.getSimpleVT(),
|
2010-05-11 23:54:07 +00:00
|
|
|
ISDOpcode,
|
|
|
|
Op0, Op0IsKill,
|
|
|
|
Op1, Op1IsKill);
|
2008-08-20 00:11:48 +00: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 00:23:20 +00:00
|
|
|
// We successfully emitted code for the given LLVM Instruction.
|
2008-09-03 23:12:08 +00:00
|
|
|
UpdateValueMap(I, ResultReg);
|
2008-08-20 00:11:48 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-04-15 01:51:59 +00:00
|
|
|
bool FastISel::SelectGetElementPtr(const User *I) {
|
2008-09-03 23:12:08 +00:00
|
|
|
unsigned N = getRegForValue(I->getOperand(0));
|
2008-08-20 22:45:34 +00:00
|
|
|
if (N == 0)
|
|
|
|
// Unhandled operand. Halt "fast" selection and bail.
|
|
|
|
return false;
|
|
|
|
|
2010-05-11 23:54:07 +00:00
|
|
|
bool NIsKill = hasTrivialKill(I->getOperand(0));
|
|
|
|
|
2011-11-17 07:15:58 +00: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 04:54:35 +00:00
|
|
|
Type *Ty = I->getOperand(0)->getType();
|
2009-08-11 20:47:22 +00:00
|
|
|
MVT VT = TLI.getPointerTy();
|
2010-04-15 01:51:59 +00: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 04:54:35 +00:00
|
|
|
if (StructType *StTy = dyn_cast<StructType>(Ty)) {
|
2008-08-20 22:45:34 +00:00
|
|
|
unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
|
|
|
|
if (Field) {
|
|
|
|
// N = N + Offset
|
2011-11-17 07:15:58 +00:00
|
|
|
TotalOffs += TD.getStructLayout(StTy)->getElementOffset(Field);
|
|
|
|
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-20 22:45:34 +00:00
|
|
|
}
|
|
|
|
Ty = StTy->getElementType(Field);
|
|
|
|
} else {
|
|
|
|
Ty = cast<SequentialType>(Ty)->getElementType();
|
|
|
|
|
|
|
|
// If this is a constant subscript, handle it quickly.
|
2010-04-15 01:51:59 +00:00
|
|
|
if (const ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
|
2010-06-18 14:22:04 +00:00
|
|
|
if (CI->isZero()) continue;
|
2011-11-17 07:15:58 +00:00
|
|
|
// N = N + Offset
|
|
|
|
TotalOffs +=
|
2009-05-09 07:06:46 +00:00
|
|
|
TD.getTypeAllocSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
|
2011-11-17 07:15:58 +00: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-20 22:45:34 +00:00
|
|
|
if (N == 0)
|
|
|
|
// Unhandled operand. Halt "fast" selection and bail.
|
|
|
|
return false;
|
2010-05-11 23:54:07 +00:00
|
|
|
NIsKill = true;
|
2011-11-17 07:15:58 +00:00
|
|
|
TotalOffs = 0;
|
2008-08-20 22:45:34 +00:00
|
|
|
}
|
2010-11-23 03:31:01 +00:00
|
|
|
|
2008-08-20 22:45:34 +00:00
|
|
|
// N = N + Idx * ElementSize;
|
2009-05-09 07:06:46 +00:00
|
|
|
uint64_t ElementSize = TD.getTypeAllocSize(Ty);
|
2010-05-11 23:54:07 +00:00
|
|
|
std::pair<unsigned, bool> Pair = getRegForGEPIndex(Idx);
|
|
|
|
unsigned IdxN = Pair.first;
|
|
|
|
bool IdxNIsKill = Pair.second;
|
2008-08-20 22:45:34 +00:00
|
|
|
if (IdxN == 0)
|
|
|
|
// Unhandled operand. Halt "fast" selection and bail.
|
|
|
|
return false;
|
|
|
|
|
2008-08-26 20:57:08 +00:00
|
|
|
if (ElementSize != 1) {
|
2010-05-11 23:54:07 +00:00
|
|
|
IdxN = FastEmit_ri_(VT, ISD::MUL, IdxN, IdxNIsKill, ElementSize, VT);
|
2008-08-26 20:57:08 +00:00
|
|
|
if (IdxN == 0)
|
|
|
|
// Unhandled operand. Halt "fast" selection and bail.
|
|
|
|
return false;
|
2010-05-11 23:54:07 +00:00
|
|
|
IdxNIsKill = true;
|
2008-08-26 20:57:08 +00:00
|
|
|
}
|
2010-05-11 23:54:07 +00:00
|
|
|
N = FastEmit_rr(VT, VT, ISD::ADD, N, NIsKill, IdxN, IdxNIsKill);
|
2008-08-20 22:45:34 +00:00
|
|
|
if (N == 0)
|
|
|
|
// Unhandled operand. Halt "fast" selection and bail.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2011-11-17 07:15:58 +00: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-20 22:45:34 +00:00
|
|
|
|
|
|
|
// We successfully emitted code for the given LLVM Instruction.
|
2008-09-03 23:12:08 +00:00
|
|
|
UpdateValueMap(I, N);
|
2008-08-20 22:45:34 +00:00
|
|
|
return true;
|
2008-08-20 00:11:48 +00:00
|
|
|
}
|
|
|
|
|
2010-04-15 01:51:59 +00:00
|
|
|
bool FastISel::SelectCall(const User *I) {
|
2011-04-26 17:18:34 +00:00
|
|
|
const CallInst *Call = cast<CallInst>(I);
|
|
|
|
|
|
|
|
// Handle simple inline asms.
|
2011-10-12 15:56:56 +00:00
|
|
|
if (const InlineAsm *IA = dyn_cast<InlineAsm>(Call->getCalledValue())) {
|
2011-04-26 17:18:34 +00: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;
|
|
|
|
|
|
|
|
BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
|
|
|
|
TII.get(TargetOpcode::INLINEASM))
|
|
|
|
.addExternalSymbol(IA->getAsmString().c_str())
|
|
|
|
.addImm(ExtraInfo);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-02-22 19:06:13 +00:00
|
|
|
MachineModuleInfo &MMI = FuncInfo.MF->getMMI();
|
|
|
|
ComputeUsesVAFloatArgument(*Call, &MMI);
|
|
|
|
|
2011-04-26 17:18:34 +00:00
|
|
|
const Function *F = Call->getCalledFunction();
|
2008-09-25 17:05:24 +00:00
|
|
|
if (!F) return false;
|
|
|
|
|
2010-04-13 17:07:06 +00:00
|
|
|
// Handle selected intrinsic function calls.
|
2011-04-19 05:52:03 +00:00
|
|
|
switch (F->getIntrinsicID()) {
|
2008-09-25 17:05:24 +00:00
|
|
|
default: break;
|
2012-02-17 23:03:39 +00:00
|
|
|
// At -O0 we don't care about the lifetime intrinsics.
|
|
|
|
case Intrinsic::lifetime_start:
|
|
|
|
case Intrinsic::lifetime_end:
|
|
|
|
return true;
|
2009-02-13 02:16:35 +00:00
|
|
|
case Intrinsic::dbg_declare: {
|
2011-04-26 17:18:34 +00:00
|
|
|
const DbgDeclareInst *DI = cast<DbgDeclareInst>(Call);
|
2010-05-07 22:04:20 +00:00
|
|
|
if (!DIVariable(DI->getVariable()).Verify() ||
|
2010-07-07 16:29:44 +00:00
|
|
|
!FuncInfo.MF->getMMI().hasDebugInfo())
|
2009-07-02 22:43:26 +00:00
|
|
|
return true;
|
2009-02-13 02:16:35 +00:00
|
|
|
|
2010-04-15 01:51:59 +00:00
|
|
|
const Value *Address = DI->getAddress();
|
2010-09-14 20:29:31 +00:00
|
|
|
if (!Address || isa<UndefValue>(Address) || isa<AllocaInst>(Address))
|
2010-02-06 02:26:02 +00:00
|
|
|
return true;
|
2010-09-14 20:29:31 +00:00
|
|
|
|
|
|
|
unsigned Reg = 0;
|
|
|
|
unsigned Offset = 0;
|
|
|
|
if (const Argument *Arg = dyn_cast<Argument>(Address)) {
|
2011-09-08 22:59:09 +00:00
|
|
|
// Some arguments' frame index is recorded during argument lowering.
|
|
|
|
Offset = FuncInfo.getArgumentFrameIndex(Arg);
|
|
|
|
if (Offset)
|
|
|
|
Reg = TRI.getFrameRegister(*FuncInfo.MF);
|
2010-09-10 20:32:09 +00:00
|
|
|
}
|
2010-09-14 20:29:31 +00:00
|
|
|
if (!Reg)
|
|
|
|
Reg = getRegForValue(Address);
|
2010-11-23 03:31:01 +00:00
|
|
|
|
2010-09-14 20:29:31 +00:00
|
|
|
if (Reg)
|
2010-11-23 03:31:01 +00:00
|
|
|
BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
|
2010-09-14 20:29:31 +00:00
|
|
|
TII.get(TargetOpcode::DBG_VALUE))
|
|
|
|
.addReg(Reg, RegState::Debug).addImm(Offset)
|
|
|
|
.addMetadata(DI->getVariable());
|
2008-09-25 17:05:24 +00:00
|
|
|
return true;
|
2009-02-13 02:16:35 +00:00
|
|
|
}
|
2010-02-26 20:01:55 +00:00
|
|
|
case Intrinsic::dbg_value: {
|
2010-04-07 01:15:14 +00:00
|
|
|
// This form of DBG_VALUE is target-independent.
|
2011-04-26 17:18:34 +00:00
|
|
|
const DbgValueInst *DI = cast<DbgValueInst>(Call);
|
2011-06-28 19:10:37 +00:00
|
|
|
const MCInstrDesc &II = TII.get(TargetOpcode::DBG_VALUE);
|
2010-04-15 01:51:59 +00:00
|
|
|
const Value *V = DI->getValue();
|
2010-02-26 20:01:55 +00:00
|
|
|
if (!V) {
|
|
|
|
// Currently the optimizer can produce this; insert an undef to
|
|
|
|
// help debugging. Probably the optimizer should not do this.
|
2010-07-10 09:00:22 +00:00
|
|
|
BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
|
|
|
|
.addReg(0U).addImm(DI->getOffset())
|
|
|
|
.addMetadata(DI->getVariable());
|
2010-04-15 01:51:59 +00:00
|
|
|
} else if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
|
2011-06-24 20:46:11 +00:00
|
|
|
if (CI->getBitWidth() > 64)
|
|
|
|
BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
|
|
|
|
.addCImm(CI).addImm(DI->getOffset())
|
|
|
|
.addMetadata(DI->getVariable());
|
|
|
|
else
|
|
|
|
BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
|
|
|
|
.addImm(CI->getZExtValue()).addImm(DI->getOffset())
|
|
|
|
.addMetadata(DI->getVariable());
|
2010-04-15 01:51:59 +00:00
|
|
|
} else if (const ConstantFP *CF = dyn_cast<ConstantFP>(V)) {
|
2010-07-10 09:00:22 +00:00
|
|
|
BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
|
|
|
|
.addFPImm(CF).addImm(DI->getOffset())
|
|
|
|
.addMetadata(DI->getVariable());
|
2010-02-26 20:01:55 +00:00
|
|
|
} else if (unsigned Reg = lookUpRegForValue(V)) {
|
2010-07-10 09:00:22 +00:00
|
|
|
BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
|
|
|
|
.addReg(Reg, RegState::Debug).addImm(DI->getOffset())
|
|
|
|
.addMetadata(DI->getVariable());
|
2010-02-26 20:01:55 +00:00
|
|
|
} else {
|
|
|
|
// We can't yet handle anything else here because it would require
|
|
|
|
// generating code, thus altering codegen because of debug info.
|
2010-12-06 22:39:26 +00:00
|
|
|
DEBUG(dbgs() << "Dropping debug info for " << DI);
|
2010-11-23 03:31:01 +00:00
|
|
|
}
|
2010-02-26 20:01:55 +00:00
|
|
|
return true;
|
|
|
|
}
|
2011-05-14 00:47:51 +00: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;
|
|
|
|
}
|
2008-09-25 17:05:24 +00:00
|
|
|
}
|
2010-04-13 17:07:06 +00:00
|
|
|
|
2011-08-18 22:06:10 +00: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.
|
|
|
|
if (!isa<IntrinsicInst>(F))
|
|
|
|
flushLocalValueMap();
|
|
|
|
|
2010-04-13 17:07:06 +00:00
|
|
|
// An arbitrary call. Bail.
|
2008-09-25 17:05:24 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-04-15 01:51:59 +00:00
|
|
|
bool FastISel::SelectCast(const User *I, unsigned Opcode) {
|
2009-08-10 22:56:29 +00:00
|
|
|
EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
|
|
|
|
EVT DstVT = TLI.getValueType(I->getType());
|
2010-11-23 03:31:01 +00:00
|
|
|
|
2009-08-11 20:47:22 +00:00
|
|
|
if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
|
|
|
|
DstVT == MVT::Other || !DstVT.isSimple())
|
2008-08-26 23:46:32 +00:00
|
|
|
// Unhandled type. Halt "fast" selection and bail.
|
|
|
|
return false;
|
2010-11-23 03:31:01 +00:00
|
|
|
|
2011-05-25 23:49:02 +00:00
|
|
|
// Check if the destination type is legal.
|
2009-03-13 23:53:06 +00:00
|
|
|
if (!TLI.isTypeLegal(DstVT))
|
2011-05-25 23:49:02 +00:00
|
|
|
return false;
|
2009-03-13 23:53:06 +00:00
|
|
|
|
2011-05-25 23:49:02 +00:00
|
|
|
// Check if the source operand is legal.
|
2009-03-13 23:53:06 +00:00
|
|
|
if (!TLI.isTypeLegal(SrcVT))
|
2011-05-25 23:49:02 +00:00
|
|
|
return false;
|
2009-03-13 23:53:06 +00:00
|
|
|
|
2008-09-03 23:12:08 +00:00
|
|
|
unsigned InputReg = getRegForValue(I->getOperand(0));
|
2008-08-26 23:46:32 +00:00
|
|
|
if (!InputReg)
|
|
|
|
// Unhandled operand. Halt "fast" selection and bail.
|
|
|
|
return false;
|
2009-03-13 20:42:20 +00:00
|
|
|
|
2010-05-11 23:54:07 +00:00
|
|
|
bool InputRegIsKill = hasTrivialKill(I->getOperand(0));
|
|
|
|
|
2008-08-26 23:46:32 +00:00
|
|
|
unsigned ResultReg = FastEmit_r(SrcVT.getSimpleVT(),
|
|
|
|
DstVT.getSimpleVT(),
|
|
|
|
Opcode,
|
2010-05-11 23:54:07 +00:00
|
|
|
InputReg, InputRegIsKill);
|
2008-08-26 23:46:32 +00:00
|
|
|
if (!ResultReg)
|
|
|
|
return false;
|
2010-11-23 03:31:01 +00:00
|
|
|
|
2008-09-03 23:12:08 +00:00
|
|
|
UpdateValueMap(I, ResultReg);
|
2008-08-26 23:46:32 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-04-15 01:51:59 +00:00
|
|
|
bool FastISel::SelectBitCast(const User *I) {
|
2008-08-27 18:10:19 +00:00
|
|
|
// If the bitcast doesn't change the type, just use the operand value.
|
|
|
|
if (I->getType() == I->getOperand(0)->getType()) {
|
2008-09-03 23:12:08 +00:00
|
|
|
unsigned Reg = getRegForValue(I->getOperand(0));
|
2008-08-27 20:41:38 +00:00
|
|
|
if (Reg == 0)
|
|
|
|
return false;
|
2008-09-03 23:12:08 +00:00
|
|
|
UpdateValueMap(I, Reg);
|
2008-08-27 18:10:19 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-11-23 03:31:01 +00:00
|
|
|
// Bitcasts of other values become reg-reg copies or BITCAST operators.
|
2009-08-10 22:56:29 +00:00
|
|
|
EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
|
|
|
|
EVT DstVT = TLI.getValueType(I->getType());
|
2010-11-23 03:31:01 +00:00
|
|
|
|
2009-08-11 20:47:22 +00:00
|
|
|
if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
|
|
|
|
DstVT == MVT::Other || !DstVT.isSimple() ||
|
2008-08-26 23:46:32 +00:00
|
|
|
!TLI.isTypeLegal(SrcVT) || !TLI.isTypeLegal(DstVT))
|
|
|
|
// Unhandled type. Halt "fast" selection and bail.
|
|
|
|
return false;
|
2010-11-23 03:31:01 +00:00
|
|
|
|
2008-09-03 23:12:08 +00:00
|
|
|
unsigned Op0 = getRegForValue(I->getOperand(0));
|
2008-08-27 18:10:19 +00:00
|
|
|
if (Op0 == 0)
|
|
|
|
// Unhandled operand. Halt "fast" selection and bail.
|
2008-08-26 23:46:32 +00:00
|
|
|
return false;
|
2010-05-11 23:54:07 +00:00
|
|
|
|
|
|
|
bool Op0IsKill = hasTrivialKill(I->getOperand(0));
|
2010-11-23 03:31:01 +00:00
|
|
|
|
2008-08-27 18:10:19 +00:00
|
|
|
// First, try to perform the bitcast by inserting a reg-reg copy.
|
|
|
|
unsigned ResultReg = 0;
|
|
|
|
if (SrcVT.getSimpleVT() == DstVT.getSimpleVT()) {
|
2012-02-22 05:59:10 +00:00
|
|
|
const TargetRegisterClass* SrcClass = TLI.getRegClassFor(SrcVT);
|
|
|
|
const TargetRegisterClass* DstClass = TLI.getRegClassFor(DstVT);
|
2010-07-11 05:16:54 +00:00
|
|
|
// Don't attempt a cross-class copy. It will likely fail.
|
|
|
|
if (SrcClass == DstClass) {
|
|
|
|
ResultReg = createResultReg(DstClass);
|
|
|
|
BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
|
|
|
|
ResultReg).addReg(Op0);
|
|
|
|
}
|
2008-08-27 18:10:19 +00:00
|
|
|
}
|
2010-11-23 03:31:01 +00:00
|
|
|
|
|
|
|
// If the reg-reg copy failed, select a BITCAST opcode.
|
2008-08-27 18:10:19 +00:00
|
|
|
if (!ResultReg)
|
|
|
|
ResultReg = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(),
|
2010-11-23 03:31:01 +00:00
|
|
|
ISD::BITCAST, Op0, Op0IsKill);
|
|
|
|
|
2008-08-27 18:10:19 +00:00
|
|
|
if (!ResultReg)
|
2008-08-26 23:46:32 +00:00
|
|
|
return false;
|
2010-11-23 03:31:01 +00:00
|
|
|
|
2008-09-03 23:12:08 +00:00
|
|
|
UpdateValueMap(I, ResultReg);
|
2008-08-26 23:46:32 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-09-03 23:12:08 +00:00
|
|
|
bool
|
2010-04-15 01:51:59 +00:00
|
|
|
FastISel::SelectInstruction(const Instruction *I) {
|
2010-04-23 15:29:50 +00: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;
|
|
|
|
|
2010-04-20 00:48:35 +00:00
|
|
|
DL = I->getDebugLoc();
|
|
|
|
|
2011-11-29 19:40:47 +00:00
|
|
|
MachineBasicBlock::iterator SavedInsertPt = FuncInfo.InsertPt;
|
|
|
|
|
2009-12-05 01:27:58 +00:00
|
|
|
// First, try doing target-independent selection.
|
2010-04-20 00:48:35 +00:00
|
|
|
if (SelectOperator(I, I->getOpcode())) {
|
2011-11-16 21:05:28 +00:00
|
|
|
++NumFastIselSuccessIndependent;
|
2010-04-20 00:48:35 +00:00
|
|
|
DL = DebugLoc();
|
2009-12-05 01:27:58 +00:00
|
|
|
return true;
|
2010-04-20 00:48:35 +00:00
|
|
|
}
|
2011-11-29 19:40:47 +00:00
|
|
|
// Remove dead code. However, ignore call instructions since we've flushed
|
|
|
|
// 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 01:27:58 +00:00
|
|
|
|
|
|
|
// Next, try calling the target to attempt to handle the instruction.
|
2011-11-29 19:40:47 +00:00
|
|
|
SavedInsertPt = FuncInfo.InsertPt;
|
2010-04-20 00:48:35 +00:00
|
|
|
if (TargetSelectInstruction(I)) {
|
2011-11-16 21:05:28 +00:00
|
|
|
++NumFastIselSuccessTarget;
|
2010-04-20 00:48:35 +00:00
|
|
|
DL = DebugLoc();
|
2009-12-05 01:27:58 +00:00
|
|
|
return true;
|
2010-04-20 00:48:35 +00:00
|
|
|
}
|
2011-11-29 19:40:47 +00:00
|
|
|
// Check for dead code and remove as necessary.
|
|
|
|
recomputeInsertPt();
|
|
|
|
if (SavedInsertPt != FuncInfo.InsertPt)
|
|
|
|
removeDeadCode(FuncInfo.InsertPt, SavedInsertPt);
|
2009-12-05 01:27:58 +00:00
|
|
|
|
2010-04-20 00:48:35 +00:00
|
|
|
DL = DebugLoc();
|
2009-12-05 01:27:58 +00:00
|
|
|
return false;
|
2008-09-05 18:18:20 +00:00
|
|
|
}
|
|
|
|
|
2008-10-02 22:15:21 +00:00
|
|
|
/// FastEmitBranch - Emit an unconditional branch to the given block,
|
|
|
|
/// unless it is the immediate (fall-through) successor, and update
|
|
|
|
/// the CFG.
|
|
|
|
void
|
2010-06-17 22:43:56 +00:00
|
|
|
FastISel::FastEmitBranch(MachineBasicBlock *MSucc, DebugLoc DL) {
|
2010-07-10 09:00:22 +00:00
|
|
|
if (FuncInfo.MBB->isLayoutSuccessor(MSucc)) {
|
2008-10-02 22:15:21 +00:00
|
|
|
// The unconditional fall-through case, which needs no instructions.
|
|
|
|
} else {
|
|
|
|
// The unconditional branch case.
|
2010-07-10 09:00:22 +00:00
|
|
|
TII.InsertBranch(*FuncInfo.MBB, MSucc, NULL,
|
|
|
|
SmallVector<MachineOperand, 0>(), DL);
|
2008-10-02 22:15:21 +00:00
|
|
|
}
|
2010-07-10 09:00:22 +00:00
|
|
|
FuncInfo.MBB->addSuccessor(MSucc);
|
2008-10-02 22:15:21 +00:00
|
|
|
}
|
|
|
|
|
2009-09-03 22:53:57 +00:00
|
|
|
/// SelectFNeg - Emit an FNeg operation.
|
|
|
|
///
|
|
|
|
bool
|
2010-04-15 01:51:59 +00:00
|
|
|
FastISel::SelectFNeg(const User *I) {
|
2009-09-03 22:53:57 +00:00
|
|
|
unsigned OpReg = getRegForValue(BinaryOperator::getFNegArgument(I));
|
|
|
|
if (OpReg == 0) return false;
|
|
|
|
|
2010-05-11 23:54:07 +00:00
|
|
|
bool OpRegIsKill = hasTrivialKill(I);
|
|
|
|
|
2009-09-11 00:36:43 +00: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-11 23:54:07 +00:00
|
|
|
ISD::FNEG, OpReg, OpRegIsKill);
|
2009-09-11 00:36:43 +00:00
|
|
|
if (ResultReg != 0) {
|
|
|
|
UpdateValueMap(I, ResultReg);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-09-11 00:34:46 +00:00
|
|
|
// Bitcast the value to integer, twiddle the sign bit with xor,
|
|
|
|
// and then bitcast it back to floating-point.
|
2009-09-03 22:53:57 +00:00
|
|
|
if (VT.getSizeInBits() > 64) return false;
|
2009-09-11 00:34:46 +00: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 03:31:01 +00:00
|
|
|
ISD::BITCAST, OpReg, OpRegIsKill);
|
2009-09-11 00:34:46 +00:00
|
|
|
if (IntReg == 0)
|
|
|
|
return false;
|
|
|
|
|
2010-05-11 23:54:07 +00:00
|
|
|
unsigned IntResultReg = FastEmit_ri_(IntVT.getSimpleVT(), ISD::XOR,
|
|
|
|
IntReg, /*Kill=*/true,
|
2009-09-11 00:34:46 +00:00
|
|
|
UINT64_C(1) << (VT.getSizeInBits()-1),
|
|
|
|
IntVT.getSimpleVT());
|
|
|
|
if (IntResultReg == 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
ResultReg = FastEmit_r(IntVT.getSimpleVT(), VT.getSimpleVT(),
|
2010-11-23 03:31:01 +00:00
|
|
|
ISD::BITCAST, IntResultReg, /*Kill=*/true);
|
2009-09-03 22:53:57 +00:00
|
|
|
if (ResultReg == 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
UpdateValueMap(I, ResultReg);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-05-16 20:27:46 +00:00
|
|
|
bool
|
|
|
|
FastISel::SelectExtractValue(const User *U) {
|
|
|
|
const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(U);
|
2011-05-16 20:34:53 +00:00
|
|
|
if (!EVI)
|
2011-05-16 20:27:46 +00:00
|
|
|
return false;
|
|
|
|
|
2011-05-16 21:06:17 +00:00
|
|
|
// Make sure we only try to handle extracts with a legal result. But also
|
|
|
|
// allow i1 because it's easy.
|
2011-05-16 20:27:46 +00:00
|
|
|
EVT RealVT = TLI.getValueType(EVI->getType(), /*AllowUnknown=*/true);
|
|
|
|
if (!RealVT.isSimple())
|
|
|
|
return false;
|
|
|
|
MVT VT = RealVT.getSimpleVT();
|
2011-05-16 21:06:17 +00:00
|
|
|
if (!TLI.isTypeLegal(VT) && VT != MVT::i1)
|
2011-05-16 20:27:46 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
const Value *Op0 = EVI->getOperand(0);
|
2011-07-18 04:54:35 +00:00
|
|
|
Type *AggTy = Op0->getType();
|
2011-05-16 20:27:46 +00: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 05:46:34 +00:00
|
|
|
else if (isa<Instruction>(Op0))
|
2011-05-16 20:27:46 +00:00
|
|
|
ResultReg = FuncInfo.InitializeRegForValue(Op0);
|
2011-06-06 05:46:34 +00:00
|
|
|
else
|
|
|
|
return false; // fast-isel can't handle aggregate constants at the moment
|
2011-05-16 20:27:46 +00:00
|
|
|
|
|
|
|
// Get the actual result register, which is an offset from the base register.
|
2011-07-13 10:26:04 +00:00
|
|
|
unsigned VTIndex = ComputeLinearIndex(AggTy, EVI->getIndices());
|
2011-05-16 20:27:46 +00: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 18:18:20 +00:00
|
|
|
bool
|
2010-04-15 01:51:59 +00:00
|
|
|
FastISel::SelectOperator(const User *I, unsigned Opcode) {
|
2008-09-05 18:18:20 +00:00
|
|
|
switch (Opcode) {
|
2009-06-04 22:49:04 +00: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-03 22:53:57 +00:00
|
|
|
// FNeg is currently represented in LLVM IR as a special case of FSub.
|
|
|
|
if (BinaryOperator::isFNeg(I))
|
|
|
|
return SelectFNeg(I);
|
2009-06-04 22:49:04 +00:00
|
|
|
return SelectBinaryOp(I, ISD::FSUB);
|
|
|
|
case Instruction::Mul:
|
|
|
|
return SelectBinaryOp(I, ISD::MUL);
|
|
|
|
case Instruction::FMul:
|
|
|
|
return SelectBinaryOp(I, ISD::FMUL);
|
2008-09-03 23:12:08 +00: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 01:51:59 +00:00
|
|
|
const BranchInst *BI = cast<BranchInst>(I);
|
2008-09-03 23:12:08 +00:00
|
|
|
|
|
|
|
if (BI->isUnconditional()) {
|
2010-04-15 01:51:59 +00:00
|
|
|
const BasicBlock *LLVMSucc = BI->getSuccessor(0);
|
2010-07-07 16:29:44 +00:00
|
|
|
MachineBasicBlock *MSucc = FuncInfo.MBBMap[LLVMSucc];
|
2010-06-17 22:43:56 +00:00
|
|
|
FastEmitBranch(MSucc, BI->getDebugLoc());
|
2008-09-03 23:12:08 +00:00
|
|
|
return true;
|
2008-08-27 00:31:01 +00:00
|
|
|
}
|
2008-09-03 23:12:08 +00:00
|
|
|
|
|
|
|
// Conditional branches are not handed yet.
|
|
|
|
// Halt "fast" selection and bail.
|
|
|
|
return false;
|
2008-08-13 20:19:35 +00:00
|
|
|
}
|
|
|
|
|
2008-09-05 01:08:41 +00:00
|
|
|
case Instruction::Unreachable:
|
|
|
|
// Nothing to emit.
|
|
|
|
return true;
|
|
|
|
|
2008-09-10 20:11:02 +00:00
|
|
|
case Instruction::Alloca:
|
|
|
|
// FunctionLowering has the static-sized case covered.
|
2010-07-07 16:29:44 +00:00
|
|
|
if (FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(I)))
|
2008-09-10 20:11:02 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
// Dynamic-sized alloca is not handled yet.
|
|
|
|
return false;
|
2010-11-23 03:31:01 +00:00
|
|
|
|
2008-09-25 17:05:24 +00:00
|
|
|
case Instruction::Call:
|
|
|
|
return SelectCall(I);
|
2010-11-23 03:31:01 +00:00
|
|
|
|
2008-09-03 23:12:08 +00: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-10 22:56:29 +00:00
|
|
|
EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
|
|
|
|
EVT DstVT = TLI.getValueType(I->getType());
|
2008-09-03 23:12:08 +00: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 21:53:34 +00:00
|
|
|
|
2011-05-16 20:27:46 +00:00
|
|
|
case Instruction::ExtractValue:
|
|
|
|
return SelectExtractValue(I);
|
|
|
|
|
2010-04-20 15:00:41 +00:00
|
|
|
case Instruction::PHI:
|
|
|
|
llvm_unreachable("FastISel shouldn't visit PHI nodes!");
|
|
|
|
|
2008-09-03 23:12:08 +00:00
|
|
|
default:
|
|
|
|
// Unhandled instruction. Halt "fast" selection and bail.
|
|
|
|
return false;
|
|
|
|
}
|
2008-08-13 20:19:35 +00:00
|
|
|
}
|
|
|
|
|
2010-07-07 16:29:44 +00:00
|
|
|
FastISel::FastISel(FunctionLoweringInfo &funcInfo)
|
2010-07-10 09:00:22 +00:00
|
|
|
: FuncInfo(funcInfo),
|
2010-07-07 16:29:44 +00:00
|
|
|
MRI(FuncInfo.MF->getRegInfo()),
|
|
|
|
MFI(*FuncInfo.MF->getFrameInfo()),
|
|
|
|
MCP(*FuncInfo.MF->getConstantPool()),
|
|
|
|
TM(FuncInfo.MF->getTarget()),
|
2008-08-22 00:20:26 +00:00
|
|
|
TD(*TM.getTargetData()),
|
|
|
|
TII(*TM.getInstrInfo()),
|
2010-05-05 23:58:35 +00:00
|
|
|
TLI(*TM.getTargetLowering()),
|
2010-07-10 09:00:22 +00:00
|
|
|
TRI(*TM.getRegisterInfo()) {
|
2008-08-20 21:05:57 +00:00
|
|
|
}
|
|
|
|
|
2008-08-14 21:51:29 +00:00
|
|
|
FastISel::~FastISel() {}
|
|
|
|
|
2009-08-11 20:47:22 +00:00
|
|
|
unsigned FastISel::FastEmit_(MVT, MVT,
|
2010-01-05 22:26:32 +00:00
|
|
|
unsigned) {
|
2008-08-13 20:19:35 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-08-11 20:47:22 +00:00
|
|
|
unsigned FastISel::FastEmit_r(MVT, MVT,
|
2010-05-11 23:54:07 +00:00
|
|
|
unsigned,
|
|
|
|
unsigned /*Op0*/, bool /*Op0IsKill*/) {
|
2008-08-13 20:19:35 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-11-23 03:31:01 +00:00
|
|
|
unsigned FastISel::FastEmit_rr(MVT, MVT,
|
2010-05-11 23:54:07 +00:00
|
|
|
unsigned,
|
|
|
|
unsigned /*Op0*/, bool /*Op0IsKill*/,
|
|
|
|
unsigned /*Op1*/, bool /*Op1IsKill*/) {
|
2008-08-13 20:19:35 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-01-05 22:26:32 +00:00
|
|
|
unsigned FastISel::FastEmit_i(MVT, MVT, unsigned, uint64_t /*Imm*/) {
|
2008-08-20 22:45:34 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-08-11 20:47:22 +00:00
|
|
|
unsigned FastISel::FastEmit_f(MVT, MVT,
|
2010-04-15 01:51:59 +00:00
|
|
|
unsigned, const ConstantFP * /*FPImm*/) {
|
2008-08-27 01:09:54 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-08-11 20:47:22 +00:00
|
|
|
unsigned FastISel::FastEmit_ri(MVT, MVT,
|
2010-05-11 23:54:07 +00:00
|
|
|
unsigned,
|
|
|
|
unsigned /*Op0*/, bool /*Op0IsKill*/,
|
2008-08-25 23:58:18 +00:00
|
|
|
uint64_t /*Imm*/) {
|
2008-08-21 01:41:07 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-08-11 20:47:22 +00:00
|
|
|
unsigned FastISel::FastEmit_rf(MVT, MVT,
|
2010-05-11 23:54:07 +00:00
|
|
|
unsigned,
|
|
|
|
unsigned /*Op0*/, bool /*Op0IsKill*/,
|
2010-04-15 01:51:59 +00:00
|
|
|
const ConstantFP * /*FPImm*/) {
|
2008-08-27 01:09:54 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-08-11 20:47:22 +00:00
|
|
|
unsigned FastISel::FastEmit_rri(MVT, MVT,
|
2010-01-05 22:26:32 +00:00
|
|
|
unsigned,
|
2010-05-11 23:54:07 +00:00
|
|
|
unsigned /*Op0*/, bool /*Op0IsKill*/,
|
|
|
|
unsigned /*Op1*/, bool /*Op1IsKill*/,
|
2008-08-21 01:41:07 +00:00
|
|
|
uint64_t /*Imm*/) {
|
2008-08-20 22:45:34 +00: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 22:26:32 +00:00
|
|
|
unsigned FastISel::FastEmit_ri_(MVT VT, unsigned Opcode,
|
2010-05-11 23:54:07 +00:00
|
|
|
unsigned Op0, bool Op0IsKill,
|
|
|
|
uint64_t Imm, MVT ImmType) {
|
2011-04-17 20:23:29 +00: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 06:55:51 +00: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 20:23:29 +00:00
|
|
|
}
|
2011-04-22 23:38:06 +00:00
|
|
|
|
2011-04-17 20:23:29 +00: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-22 23:38:06 +00:00
|
|
|
|
2008-08-20 22:45:34 +00:00
|
|
|
// First check if immediate type is legal. If not, we can't use the ri form.
|
2010-05-11 23:54:07 +00:00
|
|
|
unsigned ResultReg = FastEmit_ri(VT, VT, Opcode, Op0, Op0IsKill, Imm);
|
2008-08-20 22:45:34 +00:00
|
|
|
if (ResultReg != 0)
|
|
|
|
return ResultReg;
|
2008-08-25 23:58:18 +00:00
|
|
|
unsigned MaterialReg = FastEmit_i(ImmType, ImmType, ISD::Constant, Imm);
|
2011-04-29 23:34:52 +00: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 04:54:35 +00:00
|
|
|
IntegerType *ITy = IntegerType::get(FuncInfo.Fn->getContext(),
|
2011-04-29 23:34:52 +00:00
|
|
|
VT.getSizeInBits());
|
|
|
|
MaterialReg = getRegForValue(ConstantInt::get(ITy, Imm));
|
|
|
|
}
|
2010-05-11 23:54:07 +00:00
|
|
|
return FastEmit_rr(VT, VT, Opcode,
|
|
|
|
Op0, Op0IsKill,
|
|
|
|
MaterialReg, /*Kill=*/true);
|
2008-08-21 01:41:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned FastISel::createResultReg(const TargetRegisterClass* RC) {
|
|
|
|
return MRI.createVirtualRegister(RC);
|
2008-08-20 22:45:34 +00:00
|
|
|
}
|
|
|
|
|
2008-08-13 20:19:35 +00:00
|
|
|
unsigned FastISel::FastEmitInst_(unsigned MachineInstOpcode,
|
2008-08-20 18:09:38 +00:00
|
|
|
const TargetRegisterClass* RC) {
|
2008-08-21 01:41:07 +00:00
|
|
|
unsigned ResultReg = createResultReg(RC);
|
2011-06-28 19:10:37 +00:00
|
|
|
const MCInstrDesc &II = TII.get(MachineInstOpcode);
|
2008-08-13 20:19:35 +00:00
|
|
|
|
2010-07-10 09:00:22 +00:00
|
|
|
BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg);
|
2008-08-13 20:19:35 +00:00
|
|
|
return ResultReg;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned FastISel::FastEmitInst_r(unsigned MachineInstOpcode,
|
|
|
|
const TargetRegisterClass *RC,
|
2010-05-11 23:54:07 +00:00
|
|
|
unsigned Op0, bool Op0IsKill) {
|
2008-08-21 01:41:07 +00:00
|
|
|
unsigned ResultReg = createResultReg(RC);
|
2011-06-28 19:10:37 +00:00
|
|
|
const MCInstrDesc &II = TII.get(MachineInstOpcode);
|
2008-08-13 20:19:35 +00:00
|
|
|
|
2008-09-08 08:38:20 +00:00
|
|
|
if (II.getNumDefs() >= 1)
|
2010-07-10 09:00:22 +00:00
|
|
|
BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
|
|
|
|
.addReg(Op0, Op0IsKill * RegState::Kill);
|
2008-09-08 08:38:20 +00:00
|
|
|
else {
|
2010-07-10 09:00:22 +00:00
|
|
|
BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
|
|
|
|
.addReg(Op0, Op0IsKill * RegState::Kill);
|
2010-07-11 03:31:05 +00:00
|
|
|
BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
|
|
|
|
ResultReg).addReg(II.ImplicitDefs[0]);
|
2008-09-08 08:38:20 +00:00
|
|
|
}
|
|
|
|
|
2008-08-13 20:19:35 +00:00
|
|
|
return ResultReg;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned FastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
|
|
|
|
const TargetRegisterClass *RC,
|
2010-05-11 23:54:07 +00:00
|
|
|
unsigned Op0, bool Op0IsKill,
|
|
|
|
unsigned Op1, bool Op1IsKill) {
|
2008-08-21 01:41:07 +00:00
|
|
|
unsigned ResultReg = createResultReg(RC);
|
2011-06-28 19:10:37 +00:00
|
|
|
const MCInstrDesc &II = TII.get(MachineInstOpcode);
|
2008-08-13 20:19:35 +00:00
|
|
|
|
2008-09-08 08:38:20 +00:00
|
|
|
if (II.getNumDefs() >= 1)
|
2010-07-10 09:00:22 +00:00
|
|
|
BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
|
2010-05-11 23:54:07 +00:00
|
|
|
.addReg(Op0, Op0IsKill * RegState::Kill)
|
|
|
|
.addReg(Op1, Op1IsKill * RegState::Kill);
|
2008-09-08 08:38:20 +00:00
|
|
|
else {
|
2010-07-10 09:00:22 +00:00
|
|
|
BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
|
2010-05-11 23:54:07 +00:00
|
|
|
.addReg(Op0, Op0IsKill * RegState::Kill)
|
|
|
|
.addReg(Op1, Op1IsKill * RegState::Kill);
|
2010-07-11 03:31:05 +00:00
|
|
|
BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
|
|
|
|
ResultReg).addReg(II.ImplicitDefs[0]);
|
2008-09-08 08:38:20 +00:00
|
|
|
}
|
2008-08-13 20:19:35 +00:00
|
|
|
return ResultReg;
|
|
|
|
}
|
2008-08-21 01:41:07 +00:00
|
|
|
|
2011-05-05 17:59:04 +00: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 19:10:37 +00:00
|
|
|
const MCInstrDesc &II = TII.get(MachineInstOpcode);
|
2011-05-05 17:59:04 +00:00
|
|
|
|
|
|
|
if (II.getNumDefs() >= 1)
|
|
|
|
BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
|
|
|
|
.addReg(Op0, Op0IsKill * RegState::Kill)
|
|
|
|
.addReg(Op1, Op1IsKill * RegState::Kill)
|
|
|
|
.addReg(Op2, Op2IsKill * RegState::Kill);
|
|
|
|
else {
|
|
|
|
BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
|
|
|
|
.addReg(Op0, Op0IsKill * RegState::Kill)
|
|
|
|
.addReg(Op1, Op1IsKill * RegState::Kill)
|
|
|
|
.addReg(Op2, Op2IsKill * RegState::Kill);
|
|
|
|
BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
|
|
|
|
ResultReg).addReg(II.ImplicitDefs[0]);
|
|
|
|
}
|
|
|
|
return ResultReg;
|
|
|
|
}
|
|
|
|
|
2008-08-21 01:41:07 +00:00
|
|
|
unsigned FastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
|
|
|
|
const TargetRegisterClass *RC,
|
2010-05-11 23:54:07 +00:00
|
|
|
unsigned Op0, bool Op0IsKill,
|
|
|
|
uint64_t Imm) {
|
2008-08-21 01:41:07 +00:00
|
|
|
unsigned ResultReg = createResultReg(RC);
|
2011-06-28 19:10:37 +00:00
|
|
|
const MCInstrDesc &II = TII.get(MachineInstOpcode);
|
2008-08-21 01:41:07 +00:00
|
|
|
|
2008-09-08 08:38:20 +00:00
|
|
|
if (II.getNumDefs() >= 1)
|
2010-07-10 09:00:22 +00:00
|
|
|
BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
|
2010-05-11 23:54:07 +00:00
|
|
|
.addReg(Op0, Op0IsKill * RegState::Kill)
|
|
|
|
.addImm(Imm);
|
2008-09-08 08:38:20 +00:00
|
|
|
else {
|
2010-07-10 09:00:22 +00:00
|
|
|
BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
|
2010-05-11 23:54:07 +00:00
|
|
|
.addReg(Op0, Op0IsKill * RegState::Kill)
|
|
|
|
.addImm(Imm);
|
2010-07-11 03:31:05 +00:00
|
|
|
BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
|
|
|
|
ResultReg).addReg(II.ImplicitDefs[0]);
|
2008-09-08 08:38:20 +00:00
|
|
|
}
|
2008-08-21 01:41:07 +00:00
|
|
|
return ResultReg;
|
|
|
|
}
|
|
|
|
|
2011-03-11 21:33:55 +00: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 19:10:37 +00:00
|
|
|
const MCInstrDesc &II = TII.get(MachineInstOpcode);
|
2011-03-11 21:33:55 +00:00
|
|
|
|
|
|
|
if (II.getNumDefs() >= 1)
|
|
|
|
BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
|
|
|
|
.addReg(Op0, Op0IsKill * RegState::Kill)
|
|
|
|
.addImm(Imm1)
|
|
|
|
.addImm(Imm2);
|
|
|
|
else {
|
|
|
|
BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
|
|
|
|
.addReg(Op0, Op0IsKill * RegState::Kill)
|
|
|
|
.addImm(Imm1)
|
|
|
|
.addImm(Imm2);
|
|
|
|
BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
|
|
|
|
ResultReg).addReg(II.ImplicitDefs[0]);
|
|
|
|
}
|
|
|
|
return ResultReg;
|
|
|
|
}
|
|
|
|
|
2008-08-27 01:09:54 +00:00
|
|
|
unsigned FastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
|
|
|
|
const TargetRegisterClass *RC,
|
2010-05-11 23:54:07 +00:00
|
|
|
unsigned Op0, bool Op0IsKill,
|
|
|
|
const ConstantFP *FPImm) {
|
2008-08-27 01:09:54 +00:00
|
|
|
unsigned ResultReg = createResultReg(RC);
|
2011-06-28 19:10:37 +00:00
|
|
|
const MCInstrDesc &II = TII.get(MachineInstOpcode);
|
2008-08-27 01:09:54 +00:00
|
|
|
|
2008-09-08 08:38:20 +00:00
|
|
|
if (II.getNumDefs() >= 1)
|
2010-07-10 09:00:22 +00:00
|
|
|
BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
|
2010-05-11 23:54:07 +00:00
|
|
|
.addReg(Op0, Op0IsKill * RegState::Kill)
|
|
|
|
.addFPImm(FPImm);
|
2008-09-08 08:38:20 +00:00
|
|
|
else {
|
2010-07-10 09:00:22 +00:00
|
|
|
BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
|
2010-05-11 23:54:07 +00:00
|
|
|
.addReg(Op0, Op0IsKill * RegState::Kill)
|
|
|
|
.addFPImm(FPImm);
|
2010-07-11 03:31:05 +00:00
|
|
|
BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
|
|
|
|
ResultReg).addReg(II.ImplicitDefs[0]);
|
2008-09-08 08:38:20 +00:00
|
|
|
}
|
2008-08-27 01:09:54 +00:00
|
|
|
return ResultReg;
|
|
|
|
}
|
|
|
|
|
2008-08-21 01:41:07 +00:00
|
|
|
unsigned FastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
|
|
|
|
const TargetRegisterClass *RC,
|
2010-05-11 23:54:07 +00:00
|
|
|
unsigned Op0, bool Op0IsKill,
|
|
|
|
unsigned Op1, bool Op1IsKill,
|
|
|
|
uint64_t Imm) {
|
2008-08-21 01:41:07 +00:00
|
|
|
unsigned ResultReg = createResultReg(RC);
|
2011-06-28 19:10:37 +00:00
|
|
|
const MCInstrDesc &II = TII.get(MachineInstOpcode);
|
2008-08-21 01:41:07 +00:00
|
|
|
|
2008-09-08 08:38:20 +00:00
|
|
|
if (II.getNumDefs() >= 1)
|
2010-07-10 09:00:22 +00:00
|
|
|
BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
|
2010-05-11 23:54:07 +00:00
|
|
|
.addReg(Op0, Op0IsKill * RegState::Kill)
|
|
|
|
.addReg(Op1, Op1IsKill * RegState::Kill)
|
|
|
|
.addImm(Imm);
|
2008-09-08 08:38:20 +00:00
|
|
|
else {
|
2010-07-10 09:00:22 +00:00
|
|
|
BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
|
2010-05-11 23:54:07 +00:00
|
|
|
.addReg(Op0, Op0IsKill * RegState::Kill)
|
|
|
|
.addReg(Op1, Op1IsKill * RegState::Kill)
|
|
|
|
.addImm(Imm);
|
2010-07-11 03:31:05 +00:00
|
|
|
BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
|
|
|
|
ResultReg).addReg(II.ImplicitDefs[0]);
|
2008-09-08 08:38:20 +00:00
|
|
|
}
|
2008-08-21 01:41:07 +00:00
|
|
|
return ResultReg;
|
|
|
|
}
|
2008-08-25 20:20:32 +00:00
|
|
|
|
|
|
|
unsigned FastISel::FastEmitInst_i(unsigned MachineInstOpcode,
|
|
|
|
const TargetRegisterClass *RC,
|
|
|
|
uint64_t Imm) {
|
|
|
|
unsigned ResultReg = createResultReg(RC);
|
2011-06-28 19:10:37 +00:00
|
|
|
const MCInstrDesc &II = TII.get(MachineInstOpcode);
|
2010-11-23 03:31:01 +00:00
|
|
|
|
2008-09-08 08:38:20 +00:00
|
|
|
if (II.getNumDefs() >= 1)
|
2010-07-10 09:00:22 +00:00
|
|
|
BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg).addImm(Imm);
|
2008-09-08 08:38:20 +00:00
|
|
|
else {
|
2010-07-10 09:00:22 +00:00
|
|
|
BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II).addImm(Imm);
|
2010-07-11 03:31:05 +00:00
|
|
|
BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
|
|
|
|
ResultReg).addReg(II.ImplicitDefs[0]);
|
2008-09-08 08:38:20 +00:00
|
|
|
}
|
2008-08-25 20:20:32 +00:00
|
|
|
return ResultReg;
|
2008-08-25 22:20:39 +00:00
|
|
|
}
|
2008-08-27 22:30:02 +00:00
|
|
|
|
2011-04-22 23:38:06 +00:00
|
|
|
unsigned FastISel::FastEmitInst_ii(unsigned MachineInstOpcode,
|
|
|
|
const TargetRegisterClass *RC,
|
|
|
|
uint64_t Imm1, uint64_t Imm2) {
|
|
|
|
unsigned ResultReg = createResultReg(RC);
|
2011-06-28 19:10:37 +00:00
|
|
|
const MCInstrDesc &II = TII.get(MachineInstOpcode);
|
2011-04-22 23:38:06 +00:00
|
|
|
|
|
|
|
if (II.getNumDefs() >= 1)
|
|
|
|
BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
|
|
|
|
.addImm(Imm1).addImm(Imm2);
|
|
|
|
else {
|
|
|
|
BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II).addImm(Imm1).addImm(Imm2);
|
|
|
|
BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
|
|
|
|
ResultReg).addReg(II.ImplicitDefs[0]);
|
|
|
|
}
|
|
|
|
return ResultReg;
|
|
|
|
}
|
|
|
|
|
2009-08-11 20:47:22 +00:00
|
|
|
unsigned FastISel::FastEmitInst_extractsubreg(MVT RetVT,
|
2010-05-11 23:54:07 +00:00
|
|
|
unsigned Op0, bool Op0IsKill,
|
|
|
|
uint32_t Idx) {
|
2009-01-22 09:10:11 +00:00
|
|
|
unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
|
2010-07-08 16:40:22 +00:00
|
|
|
assert(TargetRegisterInfo::isVirtualRegister(Op0) &&
|
|
|
|
"Cannot yet extract from physregs");
|
2010-07-10 09:00:22 +00:00
|
|
|
BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
|
|
|
|
DL, TII.get(TargetOpcode::COPY), ResultReg)
|
2010-07-08 16:40:22 +00:00
|
|
|
.addReg(Op0, getKillRegState(Op0IsKill), Idx);
|
2008-08-27 22:30:02 +00:00
|
|
|
return ResultReg;
|
|
|
|
}
|
2009-03-13 20:42:20 +00:00
|
|
|
|
|
|
|
/// FastEmitZExtFromI1 - Emit MachineInstrs to compute the value of Op
|
|
|
|
/// with all but the least significant bit set to zero.
|
2010-05-11 23:54:07 +00:00
|
|
|
unsigned FastISel::FastEmitZExtFromI1(MVT VT, unsigned Op0, bool Op0IsKill) {
|
|
|
|
return FastEmit_ri(VT, VT, ISD::AND, Op0, Op0IsKill, 1);
|
2009-03-13 20:42:20 +00:00
|
|
|
}
|
2010-04-22 20:46:50 +00: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 16:29:44 +00:00
|
|
|
unsigned OrigNumPHINodesToUpdate = FuncInfo.PHINodesToUpdate.size();
|
2010-04-22 20:46:50 +00: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 16:29:44 +00:00
|
|
|
MachineBasicBlock *SuccMBB = FuncInfo.MBBMap[SuccBB];
|
2010-04-22 20:46:50 +00: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 01:10:20 +00:00
|
|
|
|
2010-04-22 20:46:50 +00: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 05:18:47 +00:00
|
|
|
// own moves. Second, this check is necessary because FastISel doesn't
|
2010-07-02 00:10:16 +00:00
|
|
|
// use CreateRegs to create registers, so it always creates
|
2010-04-22 20:46:50 +00: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 00:39:19 +00:00
|
|
|
// Handle integer promotions, though, because they're common and easy.
|
|
|
|
if (VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16)
|
2010-04-22 20:46:50 +00:00
|
|
|
VT = TLI.getTypeToTransformTo(LLVMBB->getContext(), VT);
|
|
|
|
else {
|
2010-07-07 16:29:44 +00:00
|
|
|
FuncInfo.PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
|
2010-04-22 20:46:50 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
|
|
|
|
|
2010-05-07 01:10:20 +00:00
|
|
|
// Set the DebugLoc for the copy. Prefer the location of the operand
|
|
|
|
// if there is one; use the location of the PHI otherwise.
|
|
|
|
DL = PN->getDebugLoc();
|
|
|
|
if (const Instruction *Inst = dyn_cast<Instruction>(PHIOp))
|
|
|
|
DL = Inst->getDebugLoc();
|
|
|
|
|
2010-04-22 20:46:50 +00:00
|
|
|
unsigned Reg = getRegForValue(PHIOp);
|
|
|
|
if (Reg == 0) {
|
2010-07-07 16:29:44 +00:00
|
|
|
FuncInfo.PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
|
2010-04-22 20:46:50 +00:00
|
|
|
return false;
|
|
|
|
}
|
2010-07-07 16:29:44 +00:00
|
|
|
FuncInfo.PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg));
|
2010-05-07 01:10:20 +00:00
|
|
|
DL = DebugLoc();
|
2010-04-22 20:46:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|