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