Skip to content

Commit 6235951

Browse files
committed
[CallSite removal][Instrumentation] Use CallBase instead of CallSite in AddressSanitizer/DataFlowSanitizer/MemorySanitizer. NFC
Differential Revision: https://reviews.llvm.org/D78524
1 parent a116f0f commit 6235951

File tree

3 files changed

+124
-132
lines changed

3 files changed

+124
-132
lines changed

llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
#include "llvm/IR/Argument.h"
3131
#include "llvm/IR/Attributes.h"
3232
#include "llvm/IR/BasicBlock.h"
33-
#include "llvm/IR/CallSite.h"
3433
#include "llvm/IR/Comdat.h"
3534
#include "llvm/IR/Constant.h"
3635
#include "llvm/IR/Constants.h"
@@ -1076,12 +1075,11 @@ struct FunctionStackPoisoner : public InstVisitor<FunctionStackPoisoner> {
10761075
DynamicAllocaPoisonCallVec.push_back(APC);
10771076
}
10781077

1079-
void visitCallSite(CallSite CS) {
1080-
Instruction *I = CS.getInstruction();
1081-
if (CallInst *CI = dyn_cast<CallInst>(I)) {
1078+
void visitCallBase(CallBase &CB) {
1079+
if (CallInst *CI = dyn_cast<CallInst>(&CB)) {
10821080
HasNonEmptyInlineAsm |= CI->isInlineAsm() &&
10831081
!CI->isIdenticalTo(EmptyInlineAsm.get()) &&
1084-
I != ASan.LocalDynamicShadow;
1082+
&CB != ASan.LocalDynamicShadow;
10851083
HasReturnsTwiceCall |= CI->canReturnTwice();
10861084
}
10871085
}
@@ -2685,12 +2683,11 @@ bool AddressSanitizer::instrumentFunction(Function &F,
26852683
// ok, take it.
26862684
} else {
26872685
if (isa<AllocaInst>(Inst)) NumAllocas++;
2688-
CallSite CS(&Inst);
2689-
if (CS) {
2686+
if (auto *CB = dyn_cast<CallBase>(&Inst)) {
26902687
// A call inside BB.
26912688
TempsToInstrument.clear();
2692-
if (CS.doesNotReturn() && !CS->hasMetadata("nosanitize"))
2693-
NoReturnCalls.push_back(CS.getInstruction());
2689+
if (CB->doesNotReturn() && !CB->hasMetadata("nosanitize"))
2690+
NoReturnCalls.push_back(CB);
26942691
}
26952692
if (CallInst *CI = dyn_cast<CallInst>(&Inst))
26962693
maybeMarkSanitizerLibraryCallNoBuiltin(CI, TLI);

llvm/lib/Transforms/Instrumentation/DataFlowSanitizer.cpp

Lines changed: 43 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@
5959
#include "llvm/IR/Argument.h"
6060
#include "llvm/IR/Attributes.h"
6161
#include "llvm/IR/BasicBlock.h"
62-
#include "llvm/IR/CallSite.h"
6362
#include "llvm/IR/Constant.h"
6463
#include "llvm/IR/Constants.h"
6564
#include "llvm/IR/DataLayout.h"
@@ -469,7 +468,7 @@ class DFSanVisitor : public InstVisitor<DFSanVisitor> {
469468
void visitLoadInst(LoadInst &LI);
470469
void visitStoreInst(StoreInst &SI);
471470
void visitReturnInst(ReturnInst &RI);
472-
void visitCallSite(CallSite CS);
471+
void visitCallBase(CallBase &CB);
473472
void visitPHINode(PHINode &PN);
474473
void visitExtractElementInst(ExtractElementInst &I);
475474
void visitInsertElementInst(InsertElementInst &I);
@@ -1592,10 +1591,10 @@ void DFSanVisitor::visitReturnInst(ReturnInst &RI) {
15921591
}
15931592
}
15941593

1595-
void DFSanVisitor::visitCallSite(CallSite CS) {
1596-
Function *F = CS.getCalledFunction();
1597-
if ((F && F->isIntrinsic()) || isa<InlineAsm>(CS.getCalledValue())) {
1598-
visitOperandShadowInst(*CS.getInstruction());
1594+
void DFSanVisitor::visitCallBase(CallBase &CB) {
1595+
Function *F = CB.getCalledFunction();
1596+
if ((F && F->isIntrinsic()) || isa<InlineAsm>(CB.getCalledValue())) {
1597+
visitOperandShadowInst(CB);
15991598
return;
16001599
}
16011600

@@ -1604,32 +1603,32 @@ void DFSanVisitor::visitCallSite(CallSite CS) {
16041603
if (F == DFSF.DFS.DFSanVarargWrapperFn.getCallee()->stripPointerCasts())
16051604
return;
16061605

1607-
IRBuilder<> IRB(CS.getInstruction());
1606+
IRBuilder<> IRB(&CB);
16081607

16091608
DenseMap<Value *, Function *>::iterator i =
1610-
DFSF.DFS.UnwrappedFnMap.find(CS.getCalledValue());
1609+
DFSF.DFS.UnwrappedFnMap.find(CB.getCalledValue());
16111610
if (i != DFSF.DFS.UnwrappedFnMap.end()) {
16121611
Function *F = i->second;
16131612
switch (DFSF.DFS.getWrapperKind(F)) {
16141613
case DataFlowSanitizer::WK_Warning:
1615-
CS.setCalledFunction(F);
1614+
CB.setCalledFunction(F);
16161615
IRB.CreateCall(DFSF.DFS.DFSanUnimplementedFn,
16171616
IRB.CreateGlobalStringPtr(F->getName()));
1618-
DFSF.setShadow(CS.getInstruction(), DFSF.DFS.ZeroShadow);
1617+
DFSF.setShadow(&CB, DFSF.DFS.ZeroShadow);
16191618
return;
16201619
case DataFlowSanitizer::WK_Discard:
1621-
CS.setCalledFunction(F);
1622-
DFSF.setShadow(CS.getInstruction(), DFSF.DFS.ZeroShadow);
1620+
CB.setCalledFunction(F);
1621+
DFSF.setShadow(&CB, DFSF.DFS.ZeroShadow);
16231622
return;
16241623
case DataFlowSanitizer::WK_Functional:
1625-
CS.setCalledFunction(F);
1626-
visitOperandShadowInst(*CS.getInstruction());
1624+
CB.setCalledFunction(F);
1625+
visitOperandShadowInst(CB);
16271626
return;
16281627
case DataFlowSanitizer::WK_Custom:
16291628
// Don't try to handle invokes of custom functions, it's too complicated.
16301629
// Instead, invoke the dfsw$ wrapper, which will in turn call the __dfsw_
16311630
// wrapper.
1632-
if (CallInst *CI = dyn_cast<CallInst>(CS.getInstruction())) {
1631+
if (CallInst *CI = dyn_cast<CallInst>(&CB)) {
16331632
FunctionType *FT = F->getFunctionType();
16341633
TransformedFunction CustomFn = DFSF.DFS.getCustomFunctionType(FT);
16351634
std::string CustomFName = "__dfsw_";
@@ -1648,7 +1647,7 @@ void DFSanVisitor::visitCallSite(CallSite CS) {
16481647

16491648
std::vector<Value *> Args;
16501649

1651-
CallSite::arg_iterator i = CS.arg_begin();
1650+
auto i = CB.arg_begin();
16521651
for (unsigned n = FT->getNumParams(); n != 0; ++i, --n) {
16531652
Type *T = (*i)->getType();
16541653
FunctionType *ParamFT;
@@ -1668,19 +1667,19 @@ void DFSanVisitor::visitCallSite(CallSite CS) {
16681667
}
16691668
}
16701669

1671-
i = CS.arg_begin();
1670+
i = CB.arg_begin();
16721671
const unsigned ShadowArgStart = Args.size();
16731672
for (unsigned n = FT->getNumParams(); n != 0; ++i, --n)
16741673
Args.push_back(DFSF.getShadow(*i));
16751674

16761675
if (FT->isVarArg()) {
16771676
auto *LabelVATy = ArrayType::get(DFSF.DFS.ShadowTy,
1678-
CS.arg_size() - FT->getNumParams());
1677+
CB.arg_size() - FT->getNumParams());
16791678
auto *LabelVAAlloca = new AllocaInst(
16801679
LabelVATy, getDataLayout().getAllocaAddrSpace(),
16811680
"labelva", &DFSF.F->getEntryBlock().front());
16821681

1683-
for (unsigned n = 0; i != CS.arg_end(); ++i, ++n) {
1682+
for (unsigned n = 0; i != CB.arg_end(); ++i, ++n) {
16841683
auto LabelVAPtr = IRB.CreateStructGEP(LabelVATy, LabelVAAlloca, n);
16851684
IRB.CreateStore(DFSF.getShadow(*i), LabelVAPtr);
16861685
}
@@ -1698,7 +1697,7 @@ void DFSanVisitor::visitCallSite(CallSite CS) {
16981697
Args.push_back(DFSF.LabelReturnAlloca);
16991698
}
17001699

1701-
for (i = CS.arg_begin() + FT->getNumParams(); i != CS.arg_end(); ++i)
1700+
for (i = CB.arg_begin() + FT->getNumParams(); i != CB.arg_end(); ++i)
17021701
Args.push_back(*i);
17031702

17041703
CallInst *CustomCI = IRB.CreateCall(CustomF, Args);
@@ -1730,17 +1729,17 @@ void DFSanVisitor::visitCallSite(CallSite CS) {
17301729
}
17311730

17321731
FunctionType *FT = cast<FunctionType>(
1733-
CS.getCalledValue()->getType()->getPointerElementType());
1732+
CB.getCalledValue()->getType()->getPointerElementType());
17341733
if (DFSF.DFS.getInstrumentedABI() == DataFlowSanitizer::IA_TLS) {
17351734
for (unsigned i = 0, n = FT->getNumParams(); i != n; ++i) {
1736-
IRB.CreateStore(DFSF.getShadow(CS.getArgument(i)),
1737-
DFSF.getArgTLS(i, CS.getInstruction()));
1735+
IRB.CreateStore(DFSF.getShadow(CB.getArgOperand(i)),
1736+
DFSF.getArgTLS(i, &CB));
17381737
}
17391738
}
17401739

17411740
Instruction *Next = nullptr;
1742-
if (!CS.getType()->isVoidTy()) {
1743-
if (InvokeInst *II = dyn_cast<InvokeInst>(CS.getInstruction())) {
1741+
if (!CB.getType()->isVoidTy()) {
1742+
if (InvokeInst *II = dyn_cast<InvokeInst>(&CB)) {
17441743
if (II->getNormalDest()->getSinglePredecessor()) {
17451744
Next = &II->getNormalDest()->front();
17461745
} else {
@@ -1749,15 +1748,15 @@ void DFSanVisitor::visitCallSite(CallSite CS) {
17491748
Next = &NewBB->front();
17501749
}
17511750
} else {
1752-
assert(CS->getIterator() != CS->getParent()->end());
1753-
Next = CS->getNextNode();
1751+
assert(CB.getIterator() != CB.getParent()->end());
1752+
Next = CB.getNextNode();
17541753
}
17551754

17561755
if (DFSF.DFS.getInstrumentedABI() == DataFlowSanitizer::IA_TLS) {
17571756
IRBuilder<> NextIRB(Next);
17581757
LoadInst *LI = NextIRB.CreateLoad(DFSF.DFS.ShadowTy, DFSF.getRetvalTLS());
17591758
DFSF.SkipInsts.insert(LI);
1760-
DFSF.setShadow(CS.getInstruction(), LI);
1759+
DFSF.setShadow(&CB, LI);
17611760
DFSF.NonZeroChecks.push_back(LI);
17621761
}
17631762
}
@@ -1767,58 +1766,56 @@ void DFSanVisitor::visitCallSite(CallSite CS) {
17671766
if (DFSF.DFS.getInstrumentedABI() == DataFlowSanitizer::IA_Args) {
17681767
FunctionType *NewFT = DFSF.DFS.getArgsFunctionType(FT);
17691768
Value *Func =
1770-
IRB.CreateBitCast(CS.getCalledValue(), PointerType::getUnqual(NewFT));
1769+
IRB.CreateBitCast(CB.getCalledValue(), PointerType::getUnqual(NewFT));
17711770
std::vector<Value *> Args;
17721771

1773-
CallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
1772+
auto i = CB.arg_begin(), E = CB.arg_end();
17741773
for (unsigned n = FT->getNumParams(); n != 0; ++i, --n)
17751774
Args.push_back(*i);
17761775

1777-
i = CS.arg_begin();
1776+
i = CB.arg_begin();
17781777
for (unsigned n = FT->getNumParams(); n != 0; ++i, --n)
17791778
Args.push_back(DFSF.getShadow(*i));
17801779

17811780
if (FT->isVarArg()) {
1782-
unsigned VarArgSize = CS.arg_size() - FT->getNumParams();
1781+
unsigned VarArgSize = CB.arg_size() - FT->getNumParams();
17831782
ArrayType *VarArgArrayTy = ArrayType::get(DFSF.DFS.ShadowTy, VarArgSize);
17841783
AllocaInst *VarArgShadow =
17851784
new AllocaInst(VarArgArrayTy, getDataLayout().getAllocaAddrSpace(),
17861785
"", &DFSF.F->getEntryBlock().front());
17871786
Args.push_back(IRB.CreateConstGEP2_32(VarArgArrayTy, VarArgShadow, 0, 0));
1788-
for (unsigned n = 0; i != e; ++i, ++n) {
1787+
for (unsigned n = 0; i != E; ++i, ++n) {
17891788
IRB.CreateStore(
17901789
DFSF.getShadow(*i),
17911790
IRB.CreateConstGEP2_32(VarArgArrayTy, VarArgShadow, 0, n));
17921791
Args.push_back(*i);
17931792
}
17941793
}
17951794

1796-
CallSite NewCS;
1797-
if (InvokeInst *II = dyn_cast<InvokeInst>(CS.getInstruction())) {
1798-
NewCS = IRB.CreateInvoke(NewFT, Func, II->getNormalDest(),
1795+
CallBase *NewCB;
1796+
if (InvokeInst *II = dyn_cast<InvokeInst>(&CB)) {
1797+
NewCB = IRB.CreateInvoke(NewFT, Func, II->getNormalDest(),
17991798
II->getUnwindDest(), Args);
18001799
} else {
1801-
NewCS = IRB.CreateCall(NewFT, Func, Args);
1800+
NewCB = IRB.CreateCall(NewFT, Func, Args);
18021801
}
1803-
NewCS.setCallingConv(CS.getCallingConv());
1804-
NewCS.setAttributes(CS.getAttributes().removeAttributes(
1802+
NewCB->setCallingConv(CB.getCallingConv());
1803+
NewCB->setAttributes(CB.getAttributes().removeAttributes(
18051804
*DFSF.DFS.Ctx, AttributeList::ReturnIndex,
1806-
AttributeFuncs::typeIncompatible(NewCS.getInstruction()->getType())));
1805+
AttributeFuncs::typeIncompatible(NewCB->getType())));
18071806

18081807
if (Next) {
1809-
ExtractValueInst *ExVal =
1810-
ExtractValueInst::Create(NewCS.getInstruction(), 0, "", Next);
1808+
ExtractValueInst *ExVal = ExtractValueInst::Create(NewCB, 0, "", Next);
18111809
DFSF.SkipInsts.insert(ExVal);
1812-
ExtractValueInst *ExShadow =
1813-
ExtractValueInst::Create(NewCS.getInstruction(), 1, "", Next);
1810+
ExtractValueInst *ExShadow = ExtractValueInst::Create(NewCB, 1, "", Next);
18141811
DFSF.SkipInsts.insert(ExShadow);
18151812
DFSF.setShadow(ExVal, ExShadow);
18161813
DFSF.NonZeroChecks.push_back(ExShadow);
18171814

1818-
CS.getInstruction()->replaceAllUsesWith(ExVal);
1815+
CB.replaceAllUsesWith(ExVal);
18191816
}
18201817

1821-
CS.getInstruction()->eraseFromParent();
1818+
CB.eraseFromParent();
18221819
}
18231820
}
18241821

0 commit comments

Comments
 (0)