diff --git a/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp b/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp --- a/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp +++ b/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp @@ -33,6 +33,7 @@ #include "llvm/IR/Function.h" #include "llvm/IR/IRBuilder.h" #include "llvm/IR/InlineAsm.h" +#include "llvm/IR/InstIterator.h" #include "llvm/IR/InstVisitor.h" #include "llvm/IR/Instruction.h" #include "llvm/IR/Instructions.h" @@ -1480,59 +1481,56 @@ SmallVector UnrecognizedLifetimes; DenseMap> AllocaDbgMap; bool CallsReturnTwice = false; - for (auto &BB : F) { - for (auto &Inst : BB) { - if (CallInst *CI = dyn_cast(&Inst)) { - if (CI->canReturnTwice()) { - CallsReturnTwice = true; - } + for (auto &Inst : instructions(F)) { + if (CallInst *CI = dyn_cast(&Inst)) { + if (CI->canReturnTwice()) { + CallsReturnTwice = true; + } + } + if (InstrumentStack) { + if (AllocaInst *AI = dyn_cast(&Inst)) { + if (isInterestingAlloca(*AI)) + AllocasToInstrument.insert({AI, {}}); + continue; } - if (InstrumentStack) { - if (AllocaInst *AI = dyn_cast(&Inst)) { - if (isInterestingAlloca(*AI)) - AllocasToInstrument.insert({AI, {}}); + auto *II = dyn_cast(&Inst); + if (II && (II->getIntrinsicID() == Intrinsic::lifetime_start || + II->getIntrinsicID() == Intrinsic::lifetime_end)) { + AllocaInst *AI = findAllocaForValue(II->getArgOperand(1)); + if (!AI) { + UnrecognizedLifetimes.push_back(&Inst); continue; } - auto *II = dyn_cast(&Inst); - if (II && (II->getIntrinsicID() == Intrinsic::lifetime_start || - II->getIntrinsicID() == Intrinsic::lifetime_end)) { - AllocaInst *AI = findAllocaForValue(II->getArgOperand(1)); - if (!AI) { - UnrecognizedLifetimes.push_back(&Inst); - continue; - } - if (!isInterestingAlloca(*AI)) - continue; - if (II->getIntrinsicID() == Intrinsic::lifetime_start) - AllocasToInstrument[AI].LifetimeStart.push_back(II); - else - AllocasToInstrument[AI].LifetimeEnd.push_back(II); + if (!isInterestingAlloca(*AI)) continue; - } + if (II->getIntrinsicID() == Intrinsic::lifetime_start) + AllocasToInstrument[AI].LifetimeStart.push_back(II); + else + AllocasToInstrument[AI].LifetimeEnd.push_back(II); + continue; } + } - Instruction *ExitUntag = getUntagLocationIfFunctionExit(Inst); - if (ExitUntag) - RetVec.push_back(ExitUntag); + Instruction *ExitUntag = getUntagLocationIfFunctionExit(Inst); + if (ExitUntag) + RetVec.push_back(ExitUntag); - if (auto *DVI = dyn_cast(&Inst)) { - for (Value *V : DVI->location_ops()) { - if (auto *Alloca = dyn_cast_or_null(V)) - if (!AllocaDbgMap.count(Alloca) || - AllocaDbgMap[Alloca].back() != DVI) - AllocaDbgMap[Alloca].push_back(DVI); - } + if (auto *DVI = dyn_cast(&Inst)) { + for (Value *V : DVI->location_ops()) { + if (auto *Alloca = dyn_cast_or_null(V)) + if (!AllocaDbgMap.count(Alloca) || AllocaDbgMap[Alloca].back() != DVI) + AllocaDbgMap[Alloca].push_back(DVI); } + } - if (InstrumentLandingPads && isa(Inst)) - LandingPadVec.push_back(&Inst); + if (InstrumentLandingPads && isa(Inst)) + LandingPadVec.push_back(&Inst); - getInterestingMemoryOperands(&Inst, OperandsToInstrument); + getInterestingMemoryOperands(&Inst, OperandsToInstrument); - if (MemIntrinsic *MI = dyn_cast(&Inst)) - if (!ignoreMemIntrinsic(MI)) - IntrinToInstrument.push_back(MI); - } + if (MemIntrinsic *MI = dyn_cast(&Inst)) + if (!ignoreMemIntrinsic(MI)) + IntrinToInstrument.push_back(MI); } initializeCallbacks(*F.getParent()); @@ -1580,16 +1578,14 @@ padInterestingAllocas(AllocasToInstrument); if (!AllocaToPaddedAllocaMap.empty()) { - for (auto &BB : F) { - for (auto &Inst : BB) { - if (auto *DVI = dyn_cast(&Inst)) { - SmallDenseSet LocationOps(DVI->location_ops().begin(), - DVI->location_ops().end()); - for (Value *V : LocationOps) { - if (auto *AI = dyn_cast_or_null(V)) { - if (auto *NewAI = AllocaToPaddedAllocaMap.lookup(AI)) - DVI->replaceVariableLocationOp(V, NewAI); - } + for (auto &Inst : instructions(F)) { + if (auto *DVI = dyn_cast(&Inst)) { + SmallDenseSet LocationOps(DVI->location_ops().begin(), + DVI->location_ops().end()); + for (Value *V : LocationOps) { + if (auto *AI = dyn_cast_or_null(V)) { + if (auto *NewAI = AllocaToPaddedAllocaMap.lookup(AI)) + DVI->replaceVariableLocationOp(V, NewAI); } } }