Skip to content

Commit 8ada962

Browse files
committed
[NFC] [hwasan] use InstIterator
Differential Revision: https://reviews.llvm.org/D118865
1 parent ecf132d commit 8ada962

File tree

1 file changed

+48
-52
lines changed

1 file changed

+48
-52
lines changed

llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp

Lines changed: 48 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "llvm/IR/Function.h"
3434
#include "llvm/IR/IRBuilder.h"
3535
#include "llvm/IR/InlineAsm.h"
36+
#include "llvm/IR/InstIterator.h"
3637
#include "llvm/IR/InstVisitor.h"
3738
#include "llvm/IR/Instruction.h"
3839
#include "llvm/IR/Instructions.h"
@@ -1480,59 +1481,56 @@ bool HWAddressSanitizer::sanitizeFunction(
14801481
SmallVector<Instruction *, 4> UnrecognizedLifetimes;
14811482
DenseMap<AllocaInst *, std::vector<DbgVariableIntrinsic *>> AllocaDbgMap;
14821483
bool CallsReturnTwice = false;
1483-
for (auto &BB : F) {
1484-
for (auto &Inst : BB) {
1485-
if (CallInst *CI = dyn_cast<CallInst>(&Inst)) {
1486-
if (CI->canReturnTwice()) {
1487-
CallsReturnTwice = true;
1488-
}
1484+
for (auto &Inst : instructions(F)) {
1485+
if (CallInst *CI = dyn_cast<CallInst>(&Inst)) {
1486+
if (CI->canReturnTwice()) {
1487+
CallsReturnTwice = true;
1488+
}
1489+
}
1490+
if (InstrumentStack) {
1491+
if (AllocaInst *AI = dyn_cast<AllocaInst>(&Inst)) {
1492+
if (isInterestingAlloca(*AI))
1493+
AllocasToInstrument.insert({AI, {}});
1494+
continue;
14891495
}
1490-
if (InstrumentStack) {
1491-
if (AllocaInst *AI = dyn_cast<AllocaInst>(&Inst)) {
1492-
if (isInterestingAlloca(*AI))
1493-
AllocasToInstrument.insert({AI, {}});
1496+
auto *II = dyn_cast<IntrinsicInst>(&Inst);
1497+
if (II && (II->getIntrinsicID() == Intrinsic::lifetime_start ||
1498+
II->getIntrinsicID() == Intrinsic::lifetime_end)) {
1499+
AllocaInst *AI = findAllocaForValue(II->getArgOperand(1));
1500+
if (!AI) {
1501+
UnrecognizedLifetimes.push_back(&Inst);
14941502
continue;
14951503
}
1496-
auto *II = dyn_cast<IntrinsicInst>(&Inst);
1497-
if (II && (II->getIntrinsicID() == Intrinsic::lifetime_start ||
1498-
II->getIntrinsicID() == Intrinsic::lifetime_end)) {
1499-
AllocaInst *AI = findAllocaForValue(II->getArgOperand(1));
1500-
if (!AI) {
1501-
UnrecognizedLifetimes.push_back(&Inst);
1502-
continue;
1503-
}
1504-
if (!isInterestingAlloca(*AI))
1505-
continue;
1506-
if (II->getIntrinsicID() == Intrinsic::lifetime_start)
1507-
AllocasToInstrument[AI].LifetimeStart.push_back(II);
1508-
else
1509-
AllocasToInstrument[AI].LifetimeEnd.push_back(II);
1504+
if (!isInterestingAlloca(*AI))
15101505
continue;
1511-
}
1506+
if (II->getIntrinsicID() == Intrinsic::lifetime_start)
1507+
AllocasToInstrument[AI].LifetimeStart.push_back(II);
1508+
else
1509+
AllocasToInstrument[AI].LifetimeEnd.push_back(II);
1510+
continue;
15121511
}
1512+
}
15131513

1514-
Instruction *ExitUntag = getUntagLocationIfFunctionExit(Inst);
1515-
if (ExitUntag)
1516-
RetVec.push_back(ExitUntag);
1514+
Instruction *ExitUntag = getUntagLocationIfFunctionExit(Inst);
1515+
if (ExitUntag)
1516+
RetVec.push_back(ExitUntag);
15171517

1518-
if (auto *DVI = dyn_cast<DbgVariableIntrinsic>(&Inst)) {
1519-
for (Value *V : DVI->location_ops()) {
1520-
if (auto *Alloca = dyn_cast_or_null<AllocaInst>(V))
1521-
if (!AllocaDbgMap.count(Alloca) ||
1522-
AllocaDbgMap[Alloca].back() != DVI)
1523-
AllocaDbgMap[Alloca].push_back(DVI);
1524-
}
1518+
if (auto *DVI = dyn_cast<DbgVariableIntrinsic>(&Inst)) {
1519+
for (Value *V : DVI->location_ops()) {
1520+
if (auto *Alloca = dyn_cast_or_null<AllocaInst>(V))
1521+
if (!AllocaDbgMap.count(Alloca) || AllocaDbgMap[Alloca].back() != DVI)
1522+
AllocaDbgMap[Alloca].push_back(DVI);
15251523
}
1524+
}
15261525

1527-
if (InstrumentLandingPads && isa<LandingPadInst>(Inst))
1528-
LandingPadVec.push_back(&Inst);
1526+
if (InstrumentLandingPads && isa<LandingPadInst>(Inst))
1527+
LandingPadVec.push_back(&Inst);
15291528

1530-
getInterestingMemoryOperands(&Inst, OperandsToInstrument);
1529+
getInterestingMemoryOperands(&Inst, OperandsToInstrument);
15311530

1532-
if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(&Inst))
1533-
if (!ignoreMemIntrinsic(MI))
1534-
IntrinToInstrument.push_back(MI);
1535-
}
1531+
if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(&Inst))
1532+
if (!ignoreMemIntrinsic(MI))
1533+
IntrinToInstrument.push_back(MI);
15361534
}
15371535

15381536
initializeCallbacks(*F.getParent());
@@ -1580,16 +1578,14 @@ bool HWAddressSanitizer::sanitizeFunction(
15801578
padInterestingAllocas(AllocasToInstrument);
15811579

15821580
if (!AllocaToPaddedAllocaMap.empty()) {
1583-
for (auto &BB : F) {
1584-
for (auto &Inst : BB) {
1585-
if (auto *DVI = dyn_cast<DbgVariableIntrinsic>(&Inst)) {
1586-
SmallDenseSet<Value *> LocationOps(DVI->location_ops().begin(),
1587-
DVI->location_ops().end());
1588-
for (Value *V : LocationOps) {
1589-
if (auto *AI = dyn_cast_or_null<AllocaInst>(V)) {
1590-
if (auto *NewAI = AllocaToPaddedAllocaMap.lookup(AI))
1591-
DVI->replaceVariableLocationOp(V, NewAI);
1592-
}
1581+
for (auto &Inst : instructions(F)) {
1582+
if (auto *DVI = dyn_cast<DbgVariableIntrinsic>(&Inst)) {
1583+
SmallDenseSet<Value *> LocationOps(DVI->location_ops().begin(),
1584+
DVI->location_ops().end());
1585+
for (Value *V : LocationOps) {
1586+
if (auto *AI = dyn_cast_or_null<AllocaInst>(V)) {
1587+
if (auto *NewAI = AllocaToPaddedAllocaMap.lookup(AI))
1588+
DVI->replaceVariableLocationOp(V, NewAI);
15931589
}
15941590
}
15951591
}

0 commit comments

Comments
 (0)