Skip to content

Commit

Permalink
Reduce code duplication between patchpoint and non-patchpoint lowerin…
Browse files Browse the repository at this point in the history
…g. NFC.

This is in preparation for another patch that makes patchpoints invokable.

Reviewers: atrick, ributzka
Reviewed By: ributzka
Subscribers: llvm-commits

Differential Revision: http://reviews.llvm.org/D5657

llvm-svn: 219967
  • Loading branch information
ributzka committed Oct 16, 2014
1 parent e5d8eaf commit fd4633e
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 44 deletions.
97 changes: 53 additions & 44 deletions llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
Expand Up @@ -5435,36 +5435,12 @@ SelectionDAGBuilder::visitIntrinsicCall(const CallInst &I, unsigned Intrinsic) {
}
}

void SelectionDAGBuilder::LowerCallTo(ImmutableCallSite CS, SDValue Callee,
bool isTailCall,
MachineBasicBlock *LandingPad) {
const TargetLowering &TLI = DAG.getTargetLoweringInfo();
PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
FunctionType *FTy = cast<FunctionType>(PT->getElementType());
Type *RetTy = FTy->getReturnType();
std::pair<SDValue, SDValue>
SelectionDAGBuilder::lowerInvokable(TargetLowering::CallLoweringInfo &CLI,
MachineBasicBlock *LandingPad) {
MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
MCSymbol *BeginLabel = nullptr;

TargetLowering::ArgListTy Args;
TargetLowering::ArgListEntry Entry;
Args.reserve(CS.arg_size());

for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
i != e; ++i) {
const Value *V = *i;

// Skip empty types
if (V->getType()->isEmptyTy())
continue;

SDValue ArgNode = getValue(V);
Entry.Node = ArgNode; Entry.Ty = V->getType();

// Skip the first return-type Attribute to get to params.
Entry.setAttributes(&CS, i - CS.arg_begin() + 1);
Args.push_back(Entry);
}

if (LandingPad) {
// Insert a label before the invoke call to mark the try range. This can be
// used to detect deletion of the invoke via the MachineModuleInfo.
Expand All @@ -5485,24 +5461,17 @@ void SelectionDAGBuilder::LowerCallTo(ImmutableCallSite CS, SDValue Callee,
// this call might not return.
(void)getRoot();
DAG.setRoot(DAG.getEHLabel(getCurSDLoc(), getControlRoot(), BeginLabel));
}

// Check if target-independent constraints permit a tail call here.
// Target-dependent constraints are checked within TLI.LowerCallTo.
if (isTailCall && !isInTailCallPosition(CS, DAG.getTarget()))
isTailCall = false;
CLI.setChain(getRoot());
}

TargetLowering::CallLoweringInfo CLI(DAG);
CLI.setDebugLoc(getCurSDLoc()).setChain(getRoot())
.setCallee(RetTy, FTy, Callee, std::move(Args), CS).setTailCall(isTailCall);
const TargetLowering *TLI = TM.getSubtargetImpl()->getTargetLowering();
std::pair<SDValue, SDValue> Result = TLI->LowerCallTo(CLI);

std::pair<SDValue,SDValue> Result = TLI.LowerCallTo(CLI);
assert((isTailCall || Result.second.getNode()) &&
assert((CLI.IsTailCall || Result.second.getNode()) &&
"Non-null chain expected with non-tail call!");
assert((Result.second.getNode() || !Result.first.getNode()) &&
"Null value expected with tail call!");
if (Result.first.getNode())
setValue(CS.getInstruction(), Result.first);

if (!Result.second.getNode()) {
// As a special case, a null chain means that a tail call has been emitted
Expand All @@ -5525,6 +5494,50 @@ void SelectionDAGBuilder::LowerCallTo(ImmutableCallSite CS, SDValue Callee,
// Inform MachineModuleInfo of range.
MMI.addInvoke(LandingPad, BeginLabel, EndLabel);
}

return Result;
}

void SelectionDAGBuilder::LowerCallTo(ImmutableCallSite CS, SDValue Callee,
bool isTailCall,
MachineBasicBlock *LandingPad) {
PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
FunctionType *FTy = cast<FunctionType>(PT->getElementType());
Type *RetTy = FTy->getReturnType();

TargetLowering::ArgListTy Args;
TargetLowering::ArgListEntry Entry;
Args.reserve(CS.arg_size());

for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
i != e; ++i) {
const Value *V = *i;

// Skip empty types
if (V->getType()->isEmptyTy())
continue;

SDValue ArgNode = getValue(V);
Entry.Node = ArgNode; Entry.Ty = V->getType();

// Skip the first return-type Attribute to get to params.
Entry.setAttributes(&CS, i - CS.arg_begin() + 1);
Args.push_back(Entry);
}

// Check if target-independent constraints permit a tail call here.
// Target-dependent constraints are checked within TLI->LowerCallTo.
if (isTailCall && !isInTailCallPosition(CS, DAG.getTarget()))
isTailCall = false;

TargetLowering::CallLoweringInfo CLI(DAG);
CLI.setDebugLoc(getCurSDLoc()).setChain(getRoot())
.setCallee(RetTy, FTy, Callee, std::move(Args), CS)
.setTailCall(isTailCall);
std::pair<SDValue,SDValue> Result = lowerInvokable(CLI, LandingPad);

if (Result.first.getNode())
setValue(CS.getInstruction(), Result.first);
}

/// IsOnlyUsedInZeroEqualityComparison - Return true if it only matters that the
Expand Down Expand Up @@ -6801,8 +6814,7 @@ SelectionDAGBuilder::LowerCallOperands(const CallInst &CI, unsigned ArgIdx,
.setCallee(CI.getCallingConv(), retTy, Callee, std::move(Args), NumArgs)
.setDiscardResult(!CI.use_empty());

const TargetLowering &TLI = DAG.getTargetLoweringInfo();
return TLI.LowerCallTo(CLI);
return lowerInvokable(CLI, nullptr);
}

/// \brief Add a stack map intrinsic call's live variable operands to a stackmap
Expand Down Expand Up @@ -6932,10 +6944,7 @@ void SelectionDAGBuilder::visitPatchpoint(const CallInst &CI) {
std::pair<SDValue, SDValue> Result =
LowerCallOperands(CI, NumMetaOpers, NumCallArgs, Callee, isAnyRegCC);

// Set the root to the target-lowered call chain.
SDValue Chain = Result.second;
DAG.setRoot(Chain);

SDNode *CallEnd = Chain.getNode();
if (hasDef && (CallEnd->getOpcode() == ISD::CopyFromReg))
CallEnd = CallEnd->getOperand(0).getNode();
Expand Down
5 changes: 5 additions & 0 deletions llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.h
Expand Up @@ -21,6 +21,7 @@
#include "llvm/IR/CallSite.h"
#include "llvm/IR/Constants.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Target/TargetLowering.h"
#include <vector>

namespace llvm {
Expand Down Expand Up @@ -644,6 +645,10 @@ class SelectionDAGBuilder {
void UpdateSplitBlock(MachineBasicBlock *First, MachineBasicBlock *Last);

private:
std::pair<SDValue, SDValue> lowerInvokable(
TargetLowering::CallLoweringInfo &CLI,
MachineBasicBlock *LandingPad);

// Terminator instructions.
void visitRet(const ReturnInst &I);
void visitBr(const BranchInst &I);
Expand Down

0 comments on commit fd4633e

Please sign in to comment.