[ValueTracking] Remove AllocaForValue parameter
authorVitaly Buka <vitalybuka@google.com>
Fri, 31 Jul 2020 01:48:34 +0000 (18:48 -0700)
committerVitaly Buka <vitalybuka@google.com>
Fri, 31 Jul 2020 01:48:34 +0000 (18:48 -0700)
findAllocaForValue uses AllocaForValue to cache resolved values.
The function is used only to resolve arguments of lifetime
intrinsic which usually are not fare for allocas. So result reuse
is likely unnoticeable.

In followup patches I'd like to replace the function with
GetUnderlyingObjects.

Depends on D84616.

Differential Revision: https://reviews.llvm.org/D84617

llvm/include/llvm/Analysis/ValueTracking.h
llvm/lib/Analysis/ValueTracking.cpp
llvm/lib/Target/AArch64/AArch64StackTagging.cpp
llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp
llvm/lib/Transforms/Instrumentation/MemorySanitizer.cpp

index 8008e6003e7dd25a1215514a978a4e5325c1a82a..92dc7c01e83bc8f08c4bbdc4a47515c47abd6551 100644 (file)
@@ -415,8 +415,10 @@ class Value;
                             const DataLayout &DL);
 
   /// Finds alloca where the value comes from.
-  AllocaInst *
-  findAllocaForValue(Value *V, DenseMap<Value *, AllocaInst *> &AllocaForValue);
+  AllocaInst *findAllocaForValue(Value *V);
+  inline const AllocaInst *findAllocaForValue(const Value *V) {
+    return findAllocaForValue(const_cast<Value *>(V));
+  }
 
   /// Return true if the only users of this pointer are lifetime markers.
   bool onlyUsedByLifetimeMarkers(const Value *V);
index 6fb36fa826d0948949c6334504347230521b0f65..9dfe8fdcb1a5ccbaea504c841b988610cf0dfc50 100644 (file)
@@ -4313,9 +4313,8 @@ bool llvm::getUnderlyingObjectsForCodeGen(const Value *V,
   return true;
 }
 
-AllocaInst *
-llvm::findAllocaForValue(Value *V,
-                         DenseMap<Value *, AllocaInst *> &AllocaForValue) {
+static AllocaInst *
+findAllocaForValue(Value *V, DenseMap<Value *, AllocaInst *> &AllocaForValue) {
   if (AllocaInst *AI = dyn_cast<AllocaInst>(V))
     return AI;
   // See if we've already calculated (or started to calculate) alloca for a
@@ -4348,6 +4347,11 @@ llvm::findAllocaForValue(Value *V,
   return Res;
 }
 
+AllocaInst *llvm::findAllocaForValue(Value *V) {
+  DenseMap<Value *, AllocaInst *> AllocaForValue;
+  return ::findAllocaForValue(V, AllocaForValue);
+}
+
 static bool onlyUsedByLifetimeMarkersOrDroppableInstsHelper(
     const Value *V, bool AllowLifetime, bool AllowDroppable) {
   for (const User *U : V->users()) {
index 0ed576fbc75f9bf0c6fafe6acf7415d365d61467..1b8c6417be38d834ae391d6b8fc5842b1c89aae5 100644 (file)
@@ -544,7 +544,6 @@ bool AArch64StackTagging::runOnFunction(Function &Fn) {
 
   MapVector<AllocaInst *, AllocaInfo> Allocas; // need stable iteration order
   SmallVector<Instruction *, 8> RetVec;
-  DenseMap<Value *, AllocaInst *> AllocaForValue;
   SmallVector<Instruction *, 4> UnrecognizedLifetimes;
 
   for (auto &BB : *F) {
@@ -566,8 +565,7 @@ bool AArch64StackTagging::runOnFunction(Function &Fn) {
       auto *II = dyn_cast<IntrinsicInst>(I);
       if (II && (II->getIntrinsicID() == Intrinsic::lifetime_start ||
                  II->getIntrinsicID() == Intrinsic::lifetime_end)) {
-        AllocaInst *AI =
-            llvm::findAllocaForValue(II->getArgOperand(1), AllocaForValue);
+        AllocaInst *AI = findAllocaForValue(II->getArgOperand(1));
         if (!AI) {
           UnrecognizedLifetimes.push_back(I);
           continue;
index 647d25e6a24e07c7749038078f212a68f6c81ed5..053b9aa812bf42258f29bcc1dda2dfd14c1b90da 100644 (file)
@@ -911,10 +911,6 @@ struct FunctionStackPoisoner : public InstVisitor<FunctionStackPoisoner> {
   AllocaInst *DynamicAllocaLayout = nullptr;
   IntrinsicInst *LocalEscapeCall = nullptr;
 
-  // Maps Value to an AllocaInst from which the Value is originated.
-  using AllocaForValueMapTy = DenseMap<Value *, AllocaInst *>;
-  AllocaForValueMapTy AllocaForValue;
-
   bool HasInlineAsm = false;
   bool HasReturnsTwiceCall = false;
 
@@ -1065,8 +1061,7 @@ struct FunctionStackPoisoner : public InstVisitor<FunctionStackPoisoner> {
         !ConstantInt::isValueValidForType(IntptrTy, SizeValue))
       return;
     // Find alloca instruction that corresponds to llvm.lifetime argument.
-    AllocaInst *AI =
-        llvm::findAllocaForValue(II.getArgOperand(1), AllocaForValue);
+    AllocaInst *AI = findAllocaForValue(II.getArgOperand(1));
     if (!AI) {
       HasUntracedLifetimeIntrinsic = true;
       return;
index f31d88a633b9aee6c98ee250e10c9521ba5c43d7..edde80ce0ee8fbec228aaa74e744bd3ccef3da5b 100644 (file)
@@ -2693,9 +2693,7 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
   void handleLifetimeStart(IntrinsicInst &I) {
     if (!PoisonStack)
       return;
-    DenseMap<Value *, AllocaInst *> AllocaForValue;
-    AllocaInst *AI =
-        llvm::findAllocaForValue(I.getArgOperand(1), AllocaForValue);
+    AllocaInst *AI = llvm::findAllocaForValue(I.getArgOperand(1));
     if (!AI)
       InstrumentLifetimeStart = false;
     LifetimeStartList.push_back(std::make_pair(&I, AI));