Use utostr rather than std::to_string
[oota-llvm.git] / lib / IR / Verifier.cpp
index dc723f63ea981bdafdf3a1c75fe50a824e3b6146..1557ca0dbb78821bfecf64e093cf521ce7da1e35 100644 (file)
@@ -45,6 +45,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "llvm/IR/Verifier.h"
+#include "llvm/ADT/MapVector.h"
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/ADT/SetVector.h"
 #include "llvm/ADT/SmallPtrSet.h"
@@ -145,6 +146,11 @@ private:
     OS << *C;
   }
 
+  template <typename T> void Write(ArrayRef<T> Vs) {
+    for (const T &V : Vs)
+      Write(V);
+  }
+
   template <typename T1, typename... Ts>
   void WriteTs(const T1 &V1, const Ts &... Vs) {
     Write(V1);
@@ -204,6 +210,10 @@ class Verifier : public InstVisitor<Verifier>, VerifierSupport {
   /// given function and the largest index passed to llvm.localrecover.
   DenseMap<Function *, std::pair<unsigned, unsigned>> FrameEscapeInfo;
 
+  // Maps catchswitches and cleanuppads that unwind to siblings to the
+  // terminators that indicate the unwind, used to detect cycles therein.
+  MapVector<Instruction *, TerminatorInst *> SiblingFuncletInfo;
+
   /// Cache of constants visited in search of ConstantExprs.
   SmallPtrSet<const Constant *, 32> ConstantExprVisited;
 
@@ -245,9 +255,11 @@ public:
     Broken = false;
     // FIXME: We strip const here because the inst visitor strips const.
     visit(const_cast<Function &>(F));
+    verifySiblingFuncletUnwinds();
     InstsInThisBlock.clear();
     LandingPadResultTy = nullptr;
     SawFrameEscape = false;
+    SiblingFuncletInfo.clear();
 
     return !Broken;
   }
@@ -429,12 +441,12 @@ private:
   void visitConstantExpr(const ConstantExpr *CE);
   void VerifyStatepoint(ImmutableCallSite CS);
   void verifyFrameRecoverIndices();
+  void verifySiblingFuncletUnwinds();
 
   // Module-level debug info verification...
   void verifyTypeRefs();
   template <class MapTy>
-  void verifyBitPieceExpression(const DbgInfoIntrinsic &I,
-                                const MapTy &TypeRefs);
+  void verifyDIExpression(const DbgInfoIntrinsic &I, const MapTy &TypeRefs);
   void visitUnresolvedTypeRef(const MDString *S, const MDNode *N);
 };
 } // End anonymous namespace
@@ -1697,6 +1709,59 @@ void Verifier::verifyFrameRecoverIndices() {
   }
 }
 
+static Instruction *getSuccPad(TerminatorInst *Terminator) {
+  BasicBlock *UnwindDest;
+  if (auto *II = dyn_cast<InvokeInst>(Terminator))
+    UnwindDest = II->getUnwindDest();
+  else if (auto *CSI = dyn_cast<CatchSwitchInst>(Terminator))
+    UnwindDest = CSI->getUnwindDest();
+  else
+    UnwindDest = cast<CleanupReturnInst>(Terminator)->getUnwindDest();
+  return UnwindDest->getFirstNonPHI();
+}
+
+void Verifier::verifySiblingFuncletUnwinds() {
+  SmallPtrSet<Instruction *, 8> Visited;
+  SmallPtrSet<Instruction *, 8> Active;
+  for (const auto &Pair : SiblingFuncletInfo) {
+    Instruction *PredPad = Pair.first;
+    if (Visited.count(PredPad))
+      continue;
+    Active.insert(PredPad);
+    TerminatorInst *Terminator = Pair.second;
+    do {
+      Instruction *SuccPad = getSuccPad(Terminator);
+      if (Active.count(SuccPad)) {
+        // Found a cycle; report error
+        Instruction *CyclePad = SuccPad;
+        SmallVector<Instruction *, 8> CycleNodes;
+        do {
+          CycleNodes.push_back(CyclePad);
+          TerminatorInst *CycleTerminator = SiblingFuncletInfo[CyclePad];
+          if (CycleTerminator != CyclePad)
+            CycleNodes.push_back(CycleTerminator);
+          CyclePad = getSuccPad(CycleTerminator);
+        } while (CyclePad != SuccPad);
+        Assert(false, "EH pads can't handle each other's exceptions",
+               ArrayRef<Instruction *>(CycleNodes));
+      }
+      // Don't re-walk a node we've already checked
+      if (!Visited.insert(SuccPad).second)
+        break;
+      // Walk to this successor if it has a map entry.
+      PredPad = SuccPad;
+      auto TermI = SiblingFuncletInfo.find(PredPad);
+      if (TermI == SiblingFuncletInfo.end())
+        break;
+      Terminator = TermI->second;
+      Active.insert(PredPad);
+    } while (true);
+    // Each node only has one successor, so we've walked all the active
+    // nodes' successors.
+    Active.clear();
+  }
+}
+
 // visitFunction - Verify that a function is ok.
 //
 void Verifier::visitFunction(const Function &F) {
@@ -2930,6 +2995,9 @@ void Verifier::visitEHPadPredecessors(Instruction &I) {
              "Block containg CatchPadInst must be jumped to "
              "only by its catchswitch.",
              CPI);
+    Assert(BB != CPI->getCatchSwitch()->getUnwindDest(),
+           "Catchswitch cannot unwind to one of its catchpads",
+           CPI->getCatchSwitch(), CPI);
     return;
   }
 
@@ -3150,6 +3218,10 @@ void Verifier::visitFuncletPadInst(FuncletPadInst &FPI) {
         } else {
           FirstUser = U;
           FirstUnwindPad = UnwindPad;
+          // Record cleanup sibling unwinds for verifySiblingFuncletUnwinds
+          if (isa<CleanupPadInst>(&FPI) && !isa<ConstantTokenNone>(UnwindPad) &&
+              getParentPad(UnwindPad) == getParentPad(&FPI))
+            SiblingFuncletInfo[&FPI] = cast<TerminatorInst>(U);
         }
       }
       // Make sure we visit all uses of FPI, but for nested pads stop as
@@ -3227,17 +3299,21 @@ void Verifier::visitCatchSwitchInst(CatchSwitchInst &CatchSwitch) {
          "CatchSwitchInst not the first non-PHI instruction in the block.",
          &CatchSwitch);
 
+  auto *ParentPad = CatchSwitch.getParentPad();
+  Assert(isa<ConstantTokenNone>(ParentPad) || isa<FuncletPadInst>(ParentPad),
+         "CatchSwitchInst has an invalid parent.", ParentPad);
+
   if (BasicBlock *UnwindDest = CatchSwitch.getUnwindDest()) {
     Instruction *I = UnwindDest->getFirstNonPHI();
     Assert(I->isEHPad() && !isa<LandingPadInst>(I),
            "CatchSwitchInst must unwind to an EH block which is not a "
            "landingpad.",
            &CatchSwitch);
-  }
 
-  auto *ParentPad = CatchSwitch.getParentPad();
-  Assert(isa<ConstantTokenNone>(ParentPad) || isa<FuncletPadInst>(ParentPad),
-         "CatchSwitchInst has an invalid parent.", ParentPad);
+    // Record catchswitch sibling unwinds for verifySiblingFuncletUnwinds
+    if (getParentPad(I) == ParentPad)
+      SiblingFuncletInfo[&CatchSwitch] = &CatchSwitch;
+  }
 
   Assert(CatchSwitch.getNumHandlers() != 0,
          "CatchSwitchInst cannot have empty handler list", &CatchSwitch);
@@ -4011,15 +4087,34 @@ static uint64_t getVariableSize(const DILocalVariable &V, const MapTy &Map) {
 }
 
 template <class MapTy>
-void Verifier::verifyBitPieceExpression(const DbgInfoIntrinsic &I,
-                                        const MapTy &TypeRefs) {
+void Verifier::verifyDIExpression(const DbgInfoIntrinsic &I,
+                                  const MapTy &TypeRefs) {
   DILocalVariable *V;
   DIExpression *E;
+  const Value *Arg;
+  uint64_t ArgumentTypeSizeInBits = 0;
   if (auto *DVI = dyn_cast<DbgValueInst>(&I)) {
+    Arg = DVI->getValue();
+    if (Arg)
+      ArgumentTypeSizeInBits =
+          M->getDataLayout().getTypeAllocSizeInBits(Arg->getType());
     V = dyn_cast_or_null<DILocalVariable>(DVI->getRawVariable());
     E = dyn_cast_or_null<DIExpression>(DVI->getRawExpression());
   } else {
     auto *DDI = cast<DbgDeclareInst>(&I);
+    // For declare intrinsics, get the total size of the alloca, to allow
+    // case where the variable may span more than one element.
+    Arg = DDI->getAddress();
+    if (Arg)
+      Arg = Arg->stripPointerCasts();
+    const AllocaInst *AI = dyn_cast_or_null<AllocaInst>(Arg);
+    if (AI) {
+      // We can only say something about constant size allocations
+      if (const ConstantInt *CI = dyn_cast<ConstantInt>(AI->getArraySize()))
+        ArgumentTypeSizeInBits =
+            CI->getLimitedValue() *
+            M->getDataLayout().getTypeAllocSizeInBits(AI->getAllocatedType());
+    }
     V = dyn_cast_or_null<DILocalVariable>(DDI->getRawVariable());
     E = dyn_cast_or_null<DIExpression>(DDI->getRawExpression());
   }
@@ -4028,10 +4123,6 @@ void Verifier::verifyBitPieceExpression(const DbgInfoIntrinsic &I,
   if (!V || !E || !E->isValid())
     return;
 
-  // Nothing to do if this isn't a bit piece expression.
-  if (!E->isBitPiece())
-    return;
-
   // The frontend helps out GDB by emitting the members of local anonymous
   // unions as artificial local variables with shared storage. When SROA splits
   // the storage for artificial local variables that are smaller than the entire
@@ -4047,11 +4138,33 @@ void Verifier::verifyBitPieceExpression(const DbgInfoIntrinsic &I,
   if (!VarSize)
     return;
 
-  unsigned PieceSize = E->getBitPieceSize();
-  unsigned PieceOffset = E->getBitPieceOffset();
-  Assert(PieceSize + PieceOffset <= VarSize,
-         "piece is larger than or outside of variable", &I, V, E);
-  Assert(PieceSize != VarSize, "piece covers entire variable", &I, V, E);
+  if (E->isBitPiece()) {
+    unsigned PieceSize = E->getBitPieceSize();
+    unsigned PieceOffset = E->getBitPieceOffset();
+    Assert(PieceSize + PieceOffset <= VarSize,
+           "piece is larger than or outside of variable", &I, V, E);
+    Assert(PieceSize != VarSize, "piece covers entire variable", &I, V, E);
+    return;
+  }
+
+  if (!ArgumentTypeSizeInBits)
+    return; // We were unable to determine the size of the argument
+
+  if (E->getNumElements() == 0) {
+    // In the case where the expression is empty, verify the size of the
+    // argument. Doing this in the general case would require looking through
+    // any dereferences that may be in the expression.
+    Assert(ArgumentTypeSizeInBits == VarSize,
+           "size of passed value (" + utostr(ArgumentTypeSizeInBits) +
+               ") does not match size of declared variable (" +
+               utostr(VarSize) + ")",
+           &I, Arg, V, V->getType(), E);
+  } else if (E->getElement(0) == dwarf::DW_OP_deref) {
+    Assert(ArgumentTypeSizeInBits == M->getDataLayout().getPointerSizeInBits(),
+           "the operation of the expression is a deref, but the passed value "
+           "is not pointer sized",
+           &I, Arg, V, V->getType(), E);
+  }
 }
 
 void Verifier::visitUnresolvedTypeRef(const MDString *S, const MDNode *N) {
@@ -4084,7 +4197,7 @@ void Verifier::verifyTypeRefs() {
     for (const BasicBlock &BB : F)
       for (const Instruction &I : BB)
         if (auto *DII = dyn_cast<DbgInfoIntrinsic>(&I))
-          verifyBitPieceExpression(*DII, TypeRefs);
+          verifyDIExpression(*DII, TypeRefs);
 
   // Return early if all typerefs were resolved.
   if (UnresolvedTypeRefs.empty())