2009-11-24 01:16:22 +08:00
|
|
|
//===-- FunctionLoweringInfo.cpp ------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This implements routines for translating functions from LLVM IR into
|
|
|
|
// Machine IR.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2010-07-08 00:01:37 +08:00
|
|
|
#include "llvm/CodeGen/FunctionLoweringInfo.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/CodeGen/Analysis.h"
|
|
|
|
#include "llvm/CodeGen/MachineFrameInfo.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
|
|
|
#include "llvm/CodeGen/MachineInstrBuilder.h"
|
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
2017-11-08 09:01:31 +08:00
|
|
|
#include "llvm/CodeGen/TargetFrameLowering.h"
|
|
|
|
#include "llvm/CodeGen/TargetInstrInfo.h"
|
2017-11-17 09:07:10 +08:00
|
|
|
#include "llvm/CodeGen/TargetLowering.h"
|
|
|
|
#include "llvm/CodeGen/TargetRegisterInfo.h"
|
|
|
|
#include "llvm/CodeGen/TargetSubtargetInfo.h"
|
2015-03-31 06:58:10 +08:00
|
|
|
#include "llvm/CodeGen/WinEHFuncInfo.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/DataLayout.h"
|
|
|
|
#include "llvm/IR/DerivedTypes.h"
|
|
|
|
#include "llvm/IR/Function.h"
|
|
|
|
#include "llvm/IR/Instructions.h"
|
|
|
|
#include "llvm/IR/IntrinsicInst.h"
|
|
|
|
#include "llvm/IR/LLVMContext.h"
|
|
|
|
#include "llvm/IR/Module.h"
|
2009-11-24 01:16:22 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
|
|
#include "llvm/Support/MathExtras.h"
|
2015-03-24 03:32:43 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/Target/TargetOptions.h"
|
2009-11-24 01:16:22 +08:00
|
|
|
#include <algorithm>
|
|
|
|
using namespace llvm;
|
|
|
|
|
2014-04-22 10:02:50 +08:00
|
|
|
#define DEBUG_TYPE "function-lowering-info"
|
|
|
|
|
2009-11-24 01:16:22 +08:00
|
|
|
/// isUsedOutsideOfDefiningBlock - Return true if this instruction is used by
|
|
|
|
/// PHI nodes or outside of the basic block that defines it, or used by a
|
|
|
|
/// switch or atomic instruction, which may expand to multiple basic blocks.
|
2010-04-15 12:33:49 +08:00
|
|
|
static bool isUsedOutsideOfDefiningBlock(const Instruction *I) {
|
2010-04-20 22:50:13 +08:00
|
|
|
if (I->use_empty()) return false;
|
2009-11-24 01:16:22 +08:00
|
|
|
if (isa<PHINode>(I)) return true;
|
2010-04-15 12:33:49 +08:00
|
|
|
const BasicBlock *BB = I->getParent();
|
2014-03-09 11:16:01 +08:00
|
|
|
for (const User *U : I->users())
|
2010-07-10 00:08:33 +08:00
|
|
|
if (cast<Instruction>(U)->getParent() != BB || isa<PHINode>(U))
|
2009-11-24 01:16:22 +08:00
|
|
|
return true;
|
2014-03-09 11:16:01 +08:00
|
|
|
|
2009-11-24 01:16:22 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-09-19 13:30:35 +08:00
|
|
|
static ISD::NodeType getPreferredExtendForValue(const Value *V) {
|
|
|
|
// For the users of the source value being used for compare instruction, if
|
|
|
|
// the number of signed predicate is greater than unsigned predicate, we
|
|
|
|
// prefer to use SIGN_EXTEND.
|
|
|
|
//
|
|
|
|
// With this optimization, we would be able to reduce some redundant sign or
|
|
|
|
// zero extension instruction, and eventually more machine CSE opportunities
|
|
|
|
// can be exposed.
|
|
|
|
ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
|
|
|
|
unsigned NumOfSigned = 0, NumOfUnsigned = 0;
|
|
|
|
for (const User *U : V->users()) {
|
|
|
|
if (const auto *CI = dyn_cast<CmpInst>(U)) {
|
|
|
|
NumOfSigned += CI->isSigned();
|
|
|
|
NumOfUnsigned += CI->isUnsigned();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (NumOfSigned > NumOfUnsigned)
|
|
|
|
ExtendKind = ISD::SIGN_EXTEND;
|
|
|
|
|
|
|
|
return ExtendKind;
|
|
|
|
}
|
|
|
|
|
2014-03-05 10:43:26 +08:00
|
|
|
void FunctionLoweringInfo::set(const Function &fn, MachineFunction &mf,
|
|
|
|
SelectionDAG *DAG) {
|
2009-11-24 01:16:22 +08:00
|
|
|
Fn = &fn;
|
|
|
|
MF = &mf;
|
2014-10-09 08:57:31 +08:00
|
|
|
TLI = MF->getSubtarget().getTargetLowering();
|
2009-11-24 01:16:22 +08:00
|
|
|
RegInfo = &MF->getRegInfo();
|
2015-11-28 19:02:32 +08:00
|
|
|
const TargetFrameLowering *TFI = MF->getSubtarget().getFrameLowering();
|
2016-03-03 08:01:25 +08:00
|
|
|
unsigned StackAlign = TFI->getStackAlignment();
|
2009-11-24 01:16:22 +08:00
|
|
|
|
2010-07-10 17:00:22 +08:00
|
|
|
// Check whether the function can return without sret-demotion.
|
|
|
|
SmallVector<ISD::OutputArg, 4> Outs;
|
2015-07-09 09:57:34 +08:00
|
|
|
GetReturnInfo(Fn->getReturnType(), Fn->getAttributes(), Outs, *TLI,
|
|
|
|
mf.getDataLayout());
|
2013-06-06 08:11:39 +08:00
|
|
|
CanLowerReturn = TLI->CanLowerReturn(Fn->getCallingConv(), *MF,
|
2014-10-09 08:57:31 +08:00
|
|
|
Fn->isVarArg(), Outs, Fn->getContext());
|
2010-07-10 17:00:22 +08:00
|
|
|
|
2016-03-03 08:01:25 +08:00
|
|
|
// If this personality uses funclets, we need to do a bit more work.
|
2016-10-20 01:08:23 +08:00
|
|
|
DenseMap<const AllocaInst *, TinyPtrVector<int *>> CatchObjects;
|
2016-03-03 08:01:25 +08:00
|
|
|
EHPersonality Personality = classifyEHPersonality(
|
|
|
|
Fn->hasPersonalityFn() ? Fn->getPersonalityFn() : nullptr);
|
|
|
|
if (isFuncletEHPersonality(Personality)) {
|
|
|
|
// Calculate state numbers if we haven't already.
|
|
|
|
WinEHFuncInfo &EHInfo = *MF->getWinEHFuncInfo();
|
|
|
|
if (Personality == EHPersonality::MSVC_CXX)
|
|
|
|
calculateWinCXXEHStateNumbers(&fn, EHInfo);
|
|
|
|
else if (isAsynchronousEHPersonality(Personality))
|
|
|
|
calculateSEHStateNumbers(&fn, EHInfo);
|
|
|
|
else if (Personality == EHPersonality::CoreCLR)
|
|
|
|
calculateClrEHStateNumbers(&fn, EHInfo);
|
|
|
|
|
|
|
|
// Map all BB references in the WinEH data to MBBs.
|
|
|
|
for (WinEHTryBlockMapEntry &TBME : EHInfo.TryBlockMap) {
|
|
|
|
for (WinEHHandlerType &H : TBME.HandlerArray) {
|
|
|
|
if (const AllocaInst *AI = H.CatchObj.Alloca)
|
2016-10-20 01:08:23 +08:00
|
|
|
CatchObjects.insert({AI, {}}).first->second.push_back(
|
|
|
|
&H.CatchObj.FrameIndex);
|
2016-03-03 08:01:25 +08:00
|
|
|
else
|
|
|
|
H.CatchObj.FrameIndex = INT_MAX;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-24 01:16:22 +08:00
|
|
|
// Initialize the mapping of values to registers. This is only set up for
|
|
|
|
// instruction values that are used outside of the block that defines
|
|
|
|
// them.
|
2016-12-30 08:21:38 +08:00
|
|
|
for (const BasicBlock &BB : *Fn) {
|
|
|
|
for (const Instruction &I : BB) {
|
|
|
|
if (const AllocaInst *AI = dyn_cast<AllocaInst>(&I)) {
|
2015-11-28 19:02:32 +08:00
|
|
|
Type *Ty = AI->getAllocatedType();
|
|
|
|
unsigned Align =
|
|
|
|
std::max((unsigned)MF->getDataLayout().getPrefTypeAlignment(Ty),
|
|
|
|
AI->getAlignment());
|
|
|
|
|
|
|
|
// Static allocas can be folded into the initial stack frame
|
|
|
|
// adjustment. For targets that don't realign the stack, don't
|
|
|
|
// do this if there is an extra alignment requirement.
|
2016-12-30 08:21:38 +08:00
|
|
|
if (AI->isStaticAlloca() &&
|
2015-11-28 19:02:32 +08:00
|
|
|
(TFI->isStackRealignable() || (Align <= StackAlign))) {
|
2014-09-03 02:42:44 +08:00
|
|
|
const ConstantInt *CUI = cast<ConstantInt>(AI->getArraySize());
|
2015-07-08 03:07:19 +08:00
|
|
|
uint64_t TySize = MF->getDataLayout().getTypeAllocSize(Ty);
|
2014-09-03 02:42:44 +08:00
|
|
|
|
|
|
|
TySize *= CUI->getZExtValue(); // Get total allocated size.
|
|
|
|
if (TySize == 0) TySize = 1; // Don't create zero-sized stack objects.
|
2016-03-03 08:01:25 +08:00
|
|
|
int FrameIndex = INT_MAX;
|
|
|
|
auto Iter = CatchObjects.find(AI);
|
|
|
|
if (Iter != CatchObjects.end() && TLI->needsFixedCatchObjects()) {
|
2016-07-29 02:40:00 +08:00
|
|
|
FrameIndex = MF->getFrameInfo().CreateFixedObject(
|
2016-03-03 08:01:25 +08:00
|
|
|
TySize, 0, /*Immutable=*/false, /*isAliased=*/true);
|
2016-07-29 02:40:00 +08:00
|
|
|
MF->getFrameInfo().setObjectAlignment(FrameIndex, Align);
|
2016-03-03 08:01:25 +08:00
|
|
|
} else {
|
|
|
|
FrameIndex =
|
2016-07-29 02:40:00 +08:00
|
|
|
MF->getFrameInfo().CreateStackObject(TySize, Align, false, AI);
|
2016-03-03 08:01:25 +08:00
|
|
|
}
|
2014-09-03 02:42:44 +08:00
|
|
|
|
2016-03-03 08:01:25 +08:00
|
|
|
StaticAllocaMap[AI] = FrameIndex;
|
|
|
|
// Update the catch handler information.
|
2016-10-20 01:08:23 +08:00
|
|
|
if (Iter != CatchObjects.end()) {
|
|
|
|
for (int *CatchObjPtr : Iter->second)
|
|
|
|
*CatchObjPtr = FrameIndex;
|
|
|
|
}
|
2014-09-03 02:42:44 +08:00
|
|
|
} else {
|
2015-11-28 19:02:32 +08:00
|
|
|
// FIXME: Overaligned static allocas should be grouped into
|
|
|
|
// a single dynamic allocation instead of using a separate
|
|
|
|
// stack allocation for each one.
|
2014-03-05 10:43:26 +08:00
|
|
|
if (Align <= StackAlign)
|
|
|
|
Align = 0;
|
|
|
|
// Inform the Frame Information that we have variable-sized objects.
|
2016-07-29 02:40:00 +08:00
|
|
|
MF->getFrameInfo().CreateVariableSizedObject(Align ? Align : 1, AI);
|
2014-03-05 10:43:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Look for inline asm that clobbers the SP register.
|
|
|
|
if (isa<CallInst>(I) || isa<InvokeInst>(I)) {
|
2016-12-30 08:21:38 +08:00
|
|
|
ImmutableCallSite CS(&I);
|
2014-03-05 11:21:23 +08:00
|
|
|
if (isa<InlineAsm>(CS.getCalledValue())) {
|
2014-03-05 10:43:26 +08:00
|
|
|
unsigned SP = TLI->getStackPointerRegisterToSaveRestore();
|
2015-02-27 06:38:43 +08:00
|
|
|
const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo();
|
2014-03-05 10:43:26 +08:00
|
|
|
std::vector<TargetLowering::AsmOperandInfo> Ops =
|
2015-07-08 03:07:19 +08:00
|
|
|
TLI->ParseConstraints(Fn->getParent()->getDataLayout(), TRI, CS);
|
2016-12-30 08:21:38 +08:00
|
|
|
for (TargetLowering::AsmOperandInfo &Op : Ops) {
|
2014-03-05 10:43:26 +08:00
|
|
|
if (Op.Type == InlineAsm::isClobber) {
|
|
|
|
// Clobbers don't have SDValue operands, hence SDValue().
|
|
|
|
TLI->ComputeConstraintToUse(Op, SDValue(), DAG);
|
2014-10-09 08:57:31 +08:00
|
|
|
std::pair<unsigned, const TargetRegisterClass *> PhysReg =
|
2015-02-27 06:38:43 +08:00
|
|
|
TLI->getRegForInlineAsmConstraint(TRI, Op.ConstraintCode,
|
|
|
|
Op.ConstraintVT);
|
2014-03-05 10:43:26 +08:00
|
|
|
if (PhysReg.first == SP)
|
2016-07-29 02:40:00 +08:00
|
|
|
MF->getFrameInfo().setHasOpaqueSPAdjustment(true);
|
2014-03-05 10:43:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-23 05:59:26 +08:00
|
|
|
// Look for calls to the @llvm.va_start intrinsic. We can omit some
|
|
|
|
// prologue boilerplate for variadic functions that don't examine their
|
|
|
|
// arguments.
|
2016-12-30 08:21:38 +08:00
|
|
|
if (const auto *II = dyn_cast<IntrinsicInst>(&I)) {
|
2014-08-23 05:59:26 +08:00
|
|
|
if (II->getIntrinsicID() == Intrinsic::vastart)
|
2016-07-29 02:40:00 +08:00
|
|
|
MF->getFrameInfo().setHasVAStart(true);
|
2014-08-23 05:59:26 +08:00
|
|
|
}
|
|
|
|
|
2015-12-17 07:10:53 +08:00
|
|
|
// If we have a musttail call in a variadic function, we need to ensure we
|
2014-08-30 05:42:08 +08:00
|
|
|
// forward implicit register parameters.
|
2016-12-30 08:21:38 +08:00
|
|
|
if (const auto *CI = dyn_cast<CallInst>(&I)) {
|
2014-08-30 05:42:08 +08:00
|
|
|
if (CI->isMustTailCall() && Fn->isVarArg())
|
2016-07-29 02:40:00 +08:00
|
|
|
MF->getFrameInfo().setHasMustTailInVarArgFunc(true);
|
2014-08-30 05:42:08 +08:00
|
|
|
}
|
|
|
|
|
2010-07-17 01:54:27 +08:00
|
|
|
// Mark values used outside their block as exported, by allocating
|
|
|
|
// a virtual register for them.
|
2016-12-30 08:21:38 +08:00
|
|
|
if (isUsedOutsideOfDefiningBlock(&I))
|
|
|
|
if (!isa<AllocaInst>(I) || !StaticAllocaMap.count(cast<AllocaInst>(&I)))
|
|
|
|
InitializeRegForValue(&I);
|
2009-11-24 01:16:22 +08:00
|
|
|
|
2014-09-19 13:30:35 +08:00
|
|
|
// Decide the preferred extend type for a value.
|
2016-12-30 08:21:38 +08:00
|
|
|
PreferredExtendType[&I] = getPreferredExtendForValue(&I);
|
2010-07-17 01:54:27 +08:00
|
|
|
}
|
2016-12-30 08:21:38 +08:00
|
|
|
}
|
2010-07-17 01:54:27 +08:00
|
|
|
|
2009-11-24 01:16:22 +08:00
|
|
|
// Create an initial MachineBasicBlock for each LLVM BasicBlock in F. This
|
|
|
|
// also creates the initial PHI MachineInstrs, though none of the input
|
|
|
|
// operands are populated.
|
2016-12-30 08:21:38 +08:00
|
|
|
for (const BasicBlock &BB : *Fn) {
|
2015-09-09 07:28:38 +08:00
|
|
|
// Don't create MachineBasicBlocks for imaginary EH pad blocks. These blocks
|
|
|
|
// are really data, and no instructions can live here.
|
2016-12-30 08:21:38 +08:00
|
|
|
if (BB.isEHPad()) {
|
|
|
|
const Instruction *PadInst = BB.getFirstNonPHI();
|
2015-11-06 05:09:49 +08:00
|
|
|
// If this is a non-landingpad EH pad, mark this function as using
|
2015-10-07 07:31:59 +08:00
|
|
|
// funclets.
|
2015-11-06 05:09:49 +08:00
|
|
|
// FIXME: SEH catchpads do not create funclets, so we could avoid setting
|
|
|
|
// this in such cases in order to improve frame layout.
|
2016-12-30 08:21:38 +08:00
|
|
|
if (!isa<LandingPadInst>(PadInst)) {
|
2016-12-02 03:32:15 +08:00
|
|
|
MF->setHasEHFunclets(true);
|
2016-07-29 02:40:00 +08:00
|
|
|
MF->getFrameInfo().setHasOpaqueSPAdjustment(true);
|
2015-11-06 05:09:49 +08:00
|
|
|
}
|
2016-12-30 08:21:38 +08:00
|
|
|
if (isa<CatchSwitchInst>(PadInst)) {
|
|
|
|
assert(&*BB.begin() == PadInst &&
|
2015-09-09 07:28:38 +08:00
|
|
|
"WinEHPrepare failed to remove PHIs from imaginary BBs");
|
|
|
|
continue;
|
|
|
|
}
|
2016-12-30 08:21:38 +08:00
|
|
|
if (isa<FuncletPadInst>(PadInst))
|
|
|
|
assert(&*BB.begin() == PadInst && "WinEHPrepare failed to demote PHIs");
|
2015-09-09 07:28:38 +08:00
|
|
|
}
|
|
|
|
|
2016-12-30 08:21:38 +08:00
|
|
|
MachineBasicBlock *MBB = mf.CreateMachineBasicBlock(&BB);
|
|
|
|
MBBMap[&BB] = MBB;
|
2009-11-24 01:16:22 +08:00
|
|
|
MF->push_back(MBB);
|
|
|
|
|
|
|
|
// Transfer the address-taken flag. This is necessary because there could
|
|
|
|
// be multiple MachineBasicBlocks corresponding to one BasicBlock, and only
|
|
|
|
// the first one should be marked.
|
2016-12-30 08:21:38 +08:00
|
|
|
if (BB.hasAddressTaken())
|
2009-11-24 01:16:22 +08:00
|
|
|
MBB->setHasAddressTaken();
|
|
|
|
|
2016-12-30 08:21:38 +08:00
|
|
|
// Mark landing pad blocks.
|
|
|
|
if (BB.isEHPad())
|
|
|
|
MBB->setIsEHPad();
|
|
|
|
|
2009-11-24 01:16:22 +08:00
|
|
|
// Create Machine PHI nodes for LLVM PHI nodes, lowering them as
|
|
|
|
// appropriate.
|
2017-12-30 23:27:33 +08:00
|
|
|
for (const PHINode &PN : BB.phis()) {
|
|
|
|
if (PN.use_empty())
|
|
|
|
continue;
|
2009-11-24 01:16:22 +08:00
|
|
|
|
2011-05-13 23:18:06 +08:00
|
|
|
// Skip empty types
|
2017-12-30 23:27:33 +08:00
|
|
|
if (PN.getType()->isEmptyTy())
|
2011-05-13 23:18:06 +08:00
|
|
|
continue;
|
|
|
|
|
2017-12-30 23:27:33 +08:00
|
|
|
DebugLoc DL = PN.getDebugLoc();
|
|
|
|
unsigned PHIReg = ValueMap[&PN];
|
2009-11-24 01:16:22 +08:00
|
|
|
assert(PHIReg && "PHI node does not have an assigned virtual register!");
|
|
|
|
|
|
|
|
SmallVector<EVT, 4> ValueVTs;
|
2017-12-30 23:27:33 +08:00
|
|
|
ComputeValueVTs(*TLI, MF->getDataLayout(), PN.getType(), ValueVTs);
|
2016-12-30 08:21:38 +08:00
|
|
|
for (EVT VT : ValueVTs) {
|
2013-06-06 08:11:39 +08:00
|
|
|
unsigned NumRegisters = TLI->getNumRegisters(Fn->getContext(), VT);
|
2014-08-05 10:39:49 +08:00
|
|
|
const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo();
|
2009-11-24 01:16:22 +08:00
|
|
|
for (unsigned i = 0; i != NumRegisters; ++i)
|
2010-02-10 03:54:29 +08:00
|
|
|
BuildMI(MBB, DL, TII->get(TargetOpcode::PHI), PHIReg + i);
|
2009-11-24 01:16:22 +08:00
|
|
|
PHIReg += NumRegisters;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-04-15 00:32:56 +08:00
|
|
|
|
2015-10-07 04:28:16 +08:00
|
|
|
if (!isFuncletEHPersonality(Personality))
|
2015-04-25 04:25:05 +08:00
|
|
|
return;
|
|
|
|
|
2016-01-07 12:31:35 +08:00
|
|
|
WinEHFuncInfo &EHInfo = *MF->getWinEHFuncInfo();
|
2015-09-10 08:25:23 +08:00
|
|
|
|
|
|
|
// Map all BB references in the WinEH data to MBBs.
|
2015-09-17 04:16:27 +08:00
|
|
|
for (WinEHTryBlockMapEntry &TBME : EHInfo.TryBlockMap) {
|
|
|
|
for (WinEHHandlerType &H : TBME.HandlerArray) {
|
2015-10-10 08:04:29 +08:00
|
|
|
if (H.Handler)
|
|
|
|
H.Handler = MBBMap[H.Handler.get<const BasicBlock *>()];
|
2015-09-17 04:16:27 +08:00
|
|
|
}
|
|
|
|
}
|
2015-10-10 07:34:53 +08:00
|
|
|
for (CxxUnwindMapEntry &UME : EHInfo.CxxUnwindMap)
|
2015-09-10 08:25:23 +08:00
|
|
|
if (UME.Cleanup)
|
2015-10-10 08:04:29 +08:00
|
|
|
UME.Cleanup = MBBMap[UME.Cleanup.get<const BasicBlock *>()];
|
2015-09-10 08:25:23 +08:00
|
|
|
for (SEHUnwindMapEntry &UME : EHInfo.SEHUnwindMap) {
|
|
|
|
const BasicBlock *BB = UME.Handler.get<const BasicBlock *>();
|
|
|
|
UME.Handler = MBBMap[BB];
|
2015-09-10 05:10:03 +08:00
|
|
|
}
|
2015-10-07 04:30:33 +08:00
|
|
|
for (ClrEHUnwindMapEntry &CME : EHInfo.ClrEHUnwindMap) {
|
|
|
|
const BasicBlock *BB = CME.Handler.get<const BasicBlock *>();
|
|
|
|
CME.Handler = MBBMap[BB];
|
|
|
|
}
|
2015-04-22 02:23:57 +08:00
|
|
|
}
|
|
|
|
|
2009-11-24 01:16:22 +08:00
|
|
|
/// clear - Clear out all the function-specific state. This returns this
|
|
|
|
/// FunctionLoweringInfo to an empty state, ready to be used for a
|
|
|
|
/// different function.
|
|
|
|
void FunctionLoweringInfo::clear() {
|
|
|
|
MBBMap.clear();
|
|
|
|
ValueMap.clear();
|
[AMDGPU] Fix issues for backend divergence tracking
Summary:
A change to use divergence analysis in the AMDGPU backend was getting formal
arguments incorrect (not tagged as divergent) unless they were VGPR0, VGPR1 or
VGPR2
For graphics shaders it is possible to have more than these passed in as VGPR
Modified the checking code to check for any VGPR registers passed in as formal
arguments.
Also, some intrinsics that are sources of divergence may have been lowered
during instruction selection and are missed on subsequent calls to
isSDNodeSourceOfDivergence - added the relevant AMDGPUISD checks as well.
Finally, the FunctionLoweringInfo tracks virtual registers that are live across
basic block boundaries. This is used to check for divergence of CopyFromRegister
registers using the DivergenceAnalysis analysis. For multiple blocks the lazily
evaluated inverted map VirtReg2Value was not cleared when the ValueMap map was.
Subscribers: arsenm, kzhuravl, wdng, nhaehnle, yaxunl, tpr, t-tye, llvm-commits
Differential Revision: https://reviews.llvm.org/D45372
Change-Id: I112f3bd6dfe0f62e63ce9b43b893982778e4bee3
llvm-svn: 330257
2018-04-18 21:53:31 +08:00
|
|
|
VirtReg2Value.clear();
|
2009-11-24 01:16:22 +08:00
|
|
|
StaticAllocaMap.clear();
|
|
|
|
LiveOutRegInfo.clear();
|
2011-02-24 18:00:13 +08:00
|
|
|
VisitedBBs.clear();
|
2010-04-29 07:08:54 +08:00
|
|
|
ArgDbgValues.clear();
|
2010-09-01 06:22:42 +08:00
|
|
|
ByValArgFrameIndexMap.clear();
|
2010-07-10 17:00:22 +08:00
|
|
|
RegFixups.clear();
|
[FastISel] Sink local value materializations to first use
Summary:
Local values are constants, global addresses, and stack addresses that
can't be folded into the instruction that uses them. For example, when
storing the address of a global variable into memory, we need to
materialize that address into a register.
FastISel doesn't want to materialize any given local value more than
once, so it generates all local value materialization code at
EmitStartPt, which always dominates the current insertion point. This
allows it to maintain a map of local value registers, and it knows that
the local value area will always dominate the current insertion point.
The downside is that local value instructions are always emitted without
a source location. This is done to prevent jumpy line tables, but it
means that the local value area will be considered part of the previous
statement. Consider this C code:
call1(); // line 1
++global; // line 2
++global; // line 3
call2(&global, &local); // line 4
Today we end up with assembly and line tables like this:
.loc 1 1
callq call1
leaq global(%rip), %rdi
leaq local(%rsp), %rsi
.loc 1 2
addq $1, global(%rip)
.loc 1 3
addq $1, global(%rip)
.loc 1 4
callq call2
The LEA instructions in the local value area have no source location and
are treated as being on line 1. Stepping through the code in a debugger
and correlating it with the assembly won't make much sense, because
these materializations are only required for line 4.
This is actually problematic for the VS debugger "set next statement"
feature, which effectively assumes that there are no registers live
across statement boundaries. By sinking the local value code into the
statement and fixing up the source location, we can make that feature
work. This was filed as https://bugs.llvm.org/show_bug.cgi?id=35975 and
https://crbug.com/793819.
This change is obviously not enough to make this feature work reliably
in all cases, but I felt that it was worth doing anyway because it
usually generates smaller, more comprehensible -O0 code. I measured a
0.12% regression in code generation time with LLC on the sqlite3
amalgamation, so I think this is worth doing.
There are some special cases worth calling out in the commit message:
1. local values materialized for phis
2. local values used by no-op casts
3. dead local value code
Local values can be materialized for phis, and this does not show up as
a vreg use in MachineRegisterInfo. In this case, if there are no other
uses, this patch sinks the value to the first terminator, EH label, or
the end of the BB if nothing else exists.
Local values may also be used by no-op casts, which adds the register to
the RegFixups table. Without reversing the RegFixups map direction, we
don't have enough information to sink these instructions.
Lastly, if the local value register has no other uses, we can delete it.
This comes up when fastisel tries two instruction selection approaches
and the first materializes the value but fails and the second succeeds
without using the local value.
Reviewers: aprantl, dblaikie, qcolombet, MatzeB, vsk, echristo
Subscribers: dotdash, chandlerc, hans, sdardis, amccarth, javed.absar, zturner, llvm-commits, hiraditya
Differential Revision: https://reviews.llvm.org/D43093
llvm-svn: 327581
2018-03-15 05:54:21 +08:00
|
|
|
RegsWithFixups.clear();
|
[Statepoints 3/4] Statepoint infrastructure for garbage collection: SelectionDAGBuilder
This is the third patch in a small series. It contains the CodeGen support for lowering the gc.statepoint intrinsic sequences (223078) to the STATEPOINT pseudo machine instruction (223085). The change also includes the set of helper routines and classes for working with gc.statepoints, gc.relocates, and gc.results since the lowering code uses them.
With this change, gc.statepoints should be functionally complete. The documentation will follow in the fourth change, and there will likely be some cleanup changes, but interested parties can start experimenting now.
I'm not particularly happy with the amount of code or complexity involved with the lowering step, but at least it's fairly well isolated. The statepoint lowering code is split into it's own files and anyone not working on the statepoint support itself should be able to ignore it.
During the lowering process, we currently spill aggressively to stack. This is not entirely ideal (and we have plans to do better), but it's functional, relatively straight forward, and matches closely the implementations of the patchpoint intrinsics. Most of the complexity comes from trying to keep relocated copies of values in the same stack slots across statepoints. Doing so avoids the insertion of pointless load and store instructions to reshuffle the stack. The current implementation isn't as effective as I'd like, but it is functional and 'good enough' for many common use cases.
In the long term, I'd like to figure out how to integrate the statepoint lowering with the register allocator. In principal, we shouldn't need to eagerly spill at all. The register allocator should do any spilling required and the statepoint should simply record that fact. Depending on how challenging that turns out to be, we may invest in a smarter global stack slot assignment mechanism as a stop gap measure.
Reviewed by: atrick, ributzka
llvm-svn: 223137
2014-12-03 02:50:36 +08:00
|
|
|
StatepointStackSlots.clear();
|
2016-03-25 02:57:39 +08:00
|
|
|
StatepointSpillMaps.clear();
|
2014-09-24 11:22:56 +08:00
|
|
|
PreferredExtendType.clear();
|
2009-11-24 01:16:22 +08:00
|
|
|
}
|
|
|
|
|
2010-07-02 08:10:16 +08:00
|
|
|
/// CreateReg - Allocate a single virtual register for the given type.
|
2012-12-13 14:34:11 +08:00
|
|
|
unsigned FunctionLoweringInfo::CreateReg(MVT VT) {
|
2014-08-05 05:25:23 +08:00
|
|
|
return RegInfo->createVirtualRegister(
|
2014-10-09 08:57:31 +08:00
|
|
|
MF->getSubtarget().getTargetLowering()->getRegClassFor(VT));
|
2009-11-24 01:16:22 +08:00
|
|
|
}
|
|
|
|
|
2010-07-02 08:10:16 +08:00
|
|
|
/// CreateRegs - Allocate the appropriate number of virtual registers of
|
2009-11-24 01:16:22 +08:00
|
|
|
/// the correctly promoted or expanded types. Assign these registers
|
|
|
|
/// consecutive vreg numbers and return the first assigned number.
|
|
|
|
///
|
|
|
|
/// In the case that the given value has struct or array type, this function
|
|
|
|
/// will assign registers for each member or element.
|
|
|
|
///
|
2011-07-18 12:54:35 +08:00
|
|
|
unsigned FunctionLoweringInfo::CreateRegs(Type *Ty) {
|
2014-10-09 08:57:31 +08:00
|
|
|
const TargetLowering *TLI = MF->getSubtarget().getTargetLowering();
|
2013-06-20 04:32:16 +08:00
|
|
|
|
2009-11-24 01:16:22 +08:00
|
|
|
SmallVector<EVT, 4> ValueVTs;
|
2015-07-09 09:57:34 +08:00
|
|
|
ComputeValueVTs(*TLI, MF->getDataLayout(), Ty, ValueVTs);
|
2009-11-24 01:16:22 +08:00
|
|
|
|
|
|
|
unsigned FirstReg = 0;
|
|
|
|
for (unsigned Value = 0, e = ValueVTs.size(); Value != e; ++Value) {
|
|
|
|
EVT ValueVT = ValueVTs[Value];
|
2013-06-06 08:11:39 +08:00
|
|
|
MVT RegisterVT = TLI->getRegisterType(Ty->getContext(), ValueVT);
|
2009-11-24 01:16:22 +08:00
|
|
|
|
2013-06-06 08:11:39 +08:00
|
|
|
unsigned NumRegs = TLI->getNumRegisters(Ty->getContext(), ValueVT);
|
2009-11-24 01:16:22 +08:00
|
|
|
for (unsigned i = 0; i != NumRegs; ++i) {
|
2010-07-02 08:10:16 +08:00
|
|
|
unsigned R = CreateReg(RegisterVT);
|
2009-11-24 01:16:22 +08:00
|
|
|
if (!FirstReg) FirstReg = R;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return FirstReg;
|
|
|
|
}
|
2009-11-24 01:42:46 +08:00
|
|
|
|
2011-02-24 18:00:25 +08:00
|
|
|
/// GetLiveOutRegInfo - Gets LiveOutInfo for a register, returning NULL if the
|
|
|
|
/// register is a PHI destination and the PHI's LiveOutInfo is not valid. If
|
|
|
|
/// the register's LiveOutInfo is for a smaller bit width, it is extended to
|
|
|
|
/// the larger bit width by zero extension. The bit width must be no smaller
|
|
|
|
/// than the LiveOutInfo's existing bit width.
|
|
|
|
const FunctionLoweringInfo::LiveOutInfo *
|
|
|
|
FunctionLoweringInfo::GetLiveOutRegInfo(unsigned Reg, unsigned BitWidth) {
|
|
|
|
if (!LiveOutRegInfo.inBounds(Reg))
|
2014-04-14 08:51:57 +08:00
|
|
|
return nullptr;
|
2011-02-24 18:00:25 +08:00
|
|
|
|
|
|
|
LiveOutInfo *LOI = &LiveOutRegInfo[Reg];
|
|
|
|
if (!LOI->IsValid)
|
2014-04-14 08:51:57 +08:00
|
|
|
return nullptr;
|
2011-02-24 18:00:25 +08:00
|
|
|
|
2017-04-28 13:31:46 +08:00
|
|
|
if (BitWidth > LOI->Known.getBitWidth()) {
|
2011-02-25 09:11:01 +08:00
|
|
|
LOI->NumSignBits = 1;
|
2017-05-04 06:07:25 +08:00
|
|
|
LOI->Known = LOI->Known.zextOrTrunc(BitWidth);
|
2011-02-24 18:00:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return LOI;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// ComputePHILiveOutRegInfo - Compute LiveOutInfo for a PHI's destination
|
|
|
|
/// register based on the LiveOutInfo of its operands.
|
|
|
|
void FunctionLoweringInfo::ComputePHILiveOutRegInfo(const PHINode *PN) {
|
2011-07-18 12:54:35 +08:00
|
|
|
Type *Ty = PN->getType();
|
2011-02-24 18:00:25 +08:00
|
|
|
if (!Ty->isIntegerTy() || Ty->isVectorTy())
|
|
|
|
return;
|
|
|
|
|
|
|
|
SmallVector<EVT, 1> ValueVTs;
|
2015-07-09 09:57:34 +08:00
|
|
|
ComputeValueVTs(*TLI, MF->getDataLayout(), Ty, ValueVTs);
|
2011-02-24 18:00:25 +08:00
|
|
|
assert(ValueVTs.size() == 1 &&
|
|
|
|
"PHIs with non-vector integer types should have a single VT.");
|
|
|
|
EVT IntVT = ValueVTs[0];
|
|
|
|
|
2013-06-06 08:11:39 +08:00
|
|
|
if (TLI->getNumRegisters(PN->getContext(), IntVT) != 1)
|
2011-02-24 18:00:25 +08:00
|
|
|
return;
|
2013-06-06 08:11:39 +08:00
|
|
|
IntVT = TLI->getTypeToTransformTo(PN->getContext(), IntVT);
|
2011-02-24 18:00:25 +08:00
|
|
|
unsigned BitWidth = IntVT.getSizeInBits();
|
|
|
|
|
|
|
|
unsigned DestReg = ValueMap[PN];
|
|
|
|
if (!TargetRegisterInfo::isVirtualRegister(DestReg))
|
|
|
|
return;
|
|
|
|
LiveOutRegInfo.grow(DestReg);
|
|
|
|
LiveOutInfo &DestLOI = LiveOutRegInfo[DestReg];
|
|
|
|
|
|
|
|
Value *V = PN->getIncomingValue(0);
|
|
|
|
if (isa<UndefValue>(V) || isa<ConstantExpr>(V)) {
|
|
|
|
DestLOI.NumSignBits = 1;
|
2017-04-28 13:31:46 +08:00
|
|
|
DestLOI.Known = KnownBits(BitWidth);
|
2011-02-24 18:00:25 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
|
|
|
|
APInt Val = CI->getValue().zextOrTrunc(BitWidth);
|
|
|
|
DestLOI.NumSignBits = Val.getNumSignBits();
|
2017-04-28 13:31:46 +08:00
|
|
|
DestLOI.Known.Zero = ~Val;
|
|
|
|
DestLOI.Known.One = Val;
|
2011-02-24 18:00:25 +08:00
|
|
|
} else {
|
|
|
|
assert(ValueMap.count(V) && "V should have been placed in ValueMap when its"
|
|
|
|
"CopyToReg node was created.");
|
|
|
|
unsigned SrcReg = ValueMap[V];
|
|
|
|
if (!TargetRegisterInfo::isVirtualRegister(SrcReg)) {
|
|
|
|
DestLOI.IsValid = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const LiveOutInfo *SrcLOI = GetLiveOutRegInfo(SrcReg, BitWidth);
|
|
|
|
if (!SrcLOI) {
|
|
|
|
DestLOI.IsValid = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
DestLOI = *SrcLOI;
|
|
|
|
}
|
|
|
|
|
2017-04-28 13:31:46 +08:00
|
|
|
assert(DestLOI.Known.Zero.getBitWidth() == BitWidth &&
|
|
|
|
DestLOI.Known.One.getBitWidth() == BitWidth &&
|
2011-02-24 18:00:25 +08:00
|
|
|
"Masks should have the same bit width as the type.");
|
|
|
|
|
|
|
|
for (unsigned i = 1, e = PN->getNumIncomingValues(); i != e; ++i) {
|
|
|
|
Value *V = PN->getIncomingValue(i);
|
|
|
|
if (isa<UndefValue>(V) || isa<ConstantExpr>(V)) {
|
|
|
|
DestLOI.NumSignBits = 1;
|
2017-04-28 13:31:46 +08:00
|
|
|
DestLOI.Known = KnownBits(BitWidth);
|
2011-06-09 07:55:35 +08:00
|
|
|
return;
|
2011-02-24 18:00:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
|
|
|
|
APInt Val = CI->getValue().zextOrTrunc(BitWidth);
|
|
|
|
DestLOI.NumSignBits = std::min(DestLOI.NumSignBits, Val.getNumSignBits());
|
2017-04-28 13:31:46 +08:00
|
|
|
DestLOI.Known.Zero &= ~Val;
|
|
|
|
DestLOI.Known.One &= Val;
|
2011-02-24 18:00:25 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(ValueMap.count(V) && "V should have been placed in ValueMap when "
|
|
|
|
"its CopyToReg node was created.");
|
|
|
|
unsigned SrcReg = ValueMap[V];
|
|
|
|
if (!TargetRegisterInfo::isVirtualRegister(SrcReg)) {
|
|
|
|
DestLOI.IsValid = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const LiveOutInfo *SrcLOI = GetLiveOutRegInfo(SrcReg, BitWidth);
|
|
|
|
if (!SrcLOI) {
|
|
|
|
DestLOI.IsValid = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
DestLOI.NumSignBits = std::min(DestLOI.NumSignBits, SrcLOI->NumSignBits);
|
2017-04-28 13:31:46 +08:00
|
|
|
DestLOI.Known.Zero &= SrcLOI->Known.Zero;
|
|
|
|
DestLOI.Known.One &= SrcLOI->Known.One;
|
2011-02-24 18:00:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-09 06:59:09 +08:00
|
|
|
/// setArgumentFrameIndex - Record frame index for the byval
|
2010-09-01 06:22:42 +08:00
|
|
|
/// argument. This overrides previous frame index entry for this argument,
|
|
|
|
/// if any.
|
2011-09-09 06:59:09 +08:00
|
|
|
void FunctionLoweringInfo::setArgumentFrameIndex(const Argument *A,
|
2012-02-24 09:59:01 +08:00
|
|
|
int FI) {
|
2010-09-01 06:22:42 +08:00
|
|
|
ByValArgFrameIndexMap[A] = FI;
|
|
|
|
}
|
2011-06-09 07:55:35 +08:00
|
|
|
|
2011-09-09 06:59:09 +08:00
|
|
|
/// getArgumentFrameIndex - Get frame index for the byval argument.
|
2010-09-01 06:22:42 +08:00
|
|
|
/// If the argument does not have any assigned frame index then 0 is
|
|
|
|
/// returned.
|
2011-09-09 06:59:09 +08:00
|
|
|
int FunctionLoweringInfo::getArgumentFrameIndex(const Argument *A) {
|
2017-05-10 00:02:20 +08:00
|
|
|
auto I = ByValArgFrameIndexMap.find(A);
|
2010-09-01 06:22:42 +08:00
|
|
|
if (I != ByValArgFrameIndexMap.end())
|
|
|
|
return I->second;
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "Argument does not have assigned frame index!\n");
|
2017-05-10 00:02:20 +08:00
|
|
|
return INT_MAX;
|
2010-09-01 06:22:42 +08:00
|
|
|
}
|
|
|
|
|
2015-10-07 08:27:33 +08:00
|
|
|
unsigned FunctionLoweringInfo::getCatchPadExceptionPointerVReg(
|
|
|
|
const Value *CPI, const TargetRegisterClass *RC) {
|
|
|
|
MachineRegisterInfo &MRI = MF->getRegInfo();
|
|
|
|
auto I = CatchPadExceptionPointers.insert({CPI, 0});
|
|
|
|
unsigned &VReg = I.first->second;
|
|
|
|
if (I.second)
|
|
|
|
VReg = MRI.createVirtualRegister(RC);
|
|
|
|
assert(VReg && "null vreg in exception pointer table!");
|
|
|
|
return VReg;
|
|
|
|
}
|
|
|
|
|
2016-10-08 06:06:55 +08:00
|
|
|
unsigned
|
|
|
|
FunctionLoweringInfo::getOrCreateSwiftErrorVReg(const MachineBasicBlock *MBB,
|
|
|
|
const Value *Val) {
|
|
|
|
auto Key = std::make_pair(MBB, Val);
|
|
|
|
auto It = SwiftErrorVRegDefMap.find(Key);
|
|
|
|
// If this is the first use of this swifterror value in this basic block,
|
|
|
|
// create a new virtual register.
|
|
|
|
// After we processed all basic blocks we will satisfy this "upwards exposed
|
|
|
|
// use" by inserting a copy or phi at the beginning of this block.
|
|
|
|
if (It == SwiftErrorVRegDefMap.end()) {
|
|
|
|
auto &DL = MF->getDataLayout();
|
|
|
|
const TargetRegisterClass *RC = TLI->getRegClassFor(TLI->getPointerTy(DL));
|
|
|
|
auto VReg = MF->getRegInfo().createVirtualRegister(RC);
|
|
|
|
SwiftErrorVRegDefMap[Key] = VReg;
|
|
|
|
SwiftErrorVRegUpwardsUse[Key] = VReg;
|
|
|
|
return VReg;
|
|
|
|
} else return It->second;
|
2016-04-06 02:13:16 +08:00
|
|
|
}
|
|
|
|
|
2016-10-08 06:06:55 +08:00
|
|
|
void FunctionLoweringInfo::setCurrentSwiftErrorVReg(
|
|
|
|
const MachineBasicBlock *MBB, const Value *Val, unsigned VReg) {
|
|
|
|
SwiftErrorVRegDefMap[std::make_pair(MBB, Val)] = VReg;
|
2016-04-06 02:13:16 +08:00
|
|
|
}
|
2017-06-16 01:34:42 +08:00
|
|
|
|
|
|
|
std::pair<unsigned, bool>
|
|
|
|
FunctionLoweringInfo::getOrCreateSwiftErrorVRegDefAt(const Instruction *I) {
|
|
|
|
auto Key = PointerIntPair<const Instruction *, 1, bool>(I, true);
|
|
|
|
auto It = SwiftErrorVRegDefUses.find(Key);
|
|
|
|
if (It == SwiftErrorVRegDefUses.end()) {
|
|
|
|
auto &DL = MF->getDataLayout();
|
|
|
|
const TargetRegisterClass *RC = TLI->getRegClassFor(TLI->getPointerTy(DL));
|
|
|
|
unsigned VReg = MF->getRegInfo().createVirtualRegister(RC);
|
|
|
|
SwiftErrorVRegDefUses[Key] = VReg;
|
|
|
|
return std::make_pair(VReg, true);
|
|
|
|
}
|
|
|
|
return std::make_pair(It->second, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::pair<unsigned, bool>
|
|
|
|
FunctionLoweringInfo::getOrCreateSwiftErrorVRegUseAt(const Instruction *I, const MachineBasicBlock *MBB, const Value *Val) {
|
|
|
|
auto Key = PointerIntPair<const Instruction *, 1, bool>(I, false);
|
|
|
|
auto It = SwiftErrorVRegDefUses.find(Key);
|
|
|
|
if (It == SwiftErrorVRegDefUses.end()) {
|
|
|
|
unsigned VReg = getOrCreateSwiftErrorVReg(MBB, Val);
|
|
|
|
SwiftErrorVRegDefUses[Key] = VReg;
|
|
|
|
return std::make_pair(VReg, true);
|
|
|
|
}
|
|
|
|
return std::make_pair(It->second, false);
|
|
|
|
}
|
2018-03-05 23:12:21 +08:00
|
|
|
|
|
|
|
const Value *
|
|
|
|
FunctionLoweringInfo::getValueFromVirtualReg(unsigned Vreg) {
|
|
|
|
if (VirtReg2Value.empty()) {
|
|
|
|
for (auto &P : ValueMap) {
|
|
|
|
VirtReg2Value[P.second] = P.first;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return VirtReg2Value[Vreg];
|
|
|
|
}
|