[llvm] r314763 - [Lint] Avoid failed assertion by fetching the proper pointer type

Mikael Holmen via llvm-commits llvm-commits at lists.llvm.org
Mon Oct 2 23:03:49 PDT 2017


Author: uabelho
Date: Mon Oct  2 23:03:49 2017
New Revision: 314763

URL: http://llvm.org/viewvc/llvm-project?rev=314763&view=rev
Log:
[Lint] Avoid failed assertion by fetching the proper pointer type

Summary:
When checking if a constant expression is a noop cast we fetched the
IntPtrType by doing DL->getIntPtrType(V->getType())). However, there can
be cases where V doesn't return a pointer, and then getIntPtrType()
triggers an assertion.

Now we pass DataLayout to isNoopCast so the method itself can determine
what the IntPtrType is.

Reviewers: arsenm

Reviewed By: arsenm

Subscribers: wdng, llvm-commits

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

Added:
    llvm/trunk/test/Analysis/Lint/noop-cast-expr-no-pointer.ll
Modified:
    llvm/trunk/include/llvm/IR/InstrTypes.h
    llvm/trunk/lib/Analysis/Lint.cpp
    llvm/trunk/lib/IR/Instructions.cpp

Modified: llvm/trunk/include/llvm/IR/InstrTypes.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/IR/InstrTypes.h?rev=314763&r1=314762&r2=314763&view=diff
==============================================================================
--- llvm/trunk/include/llvm/IR/InstrTypes.h (original)
+++ llvm/trunk/include/llvm/IR/InstrTypes.h Mon Oct  2 23:03:49 2017
@@ -789,6 +789,14 @@ public:
     Type *IntPtrTy ///< Integer type corresponding to Ptr types
   );
 
+  /// @brief Determine if the described cast is a no-op cast.
+  static bool isNoopCast(
+    Instruction::CastOps Opcode, ///< Opcode of cast
+    Type *SrcTy,         ///< SrcTy of cast
+    Type *DstTy,         ///< DstTy of cast
+    const DataLayout &DL ///< DataLayout to get the Int Ptr type from.
+  );
+
   /// @brief Determine if this cast is a no-op cast.
   bool isNoopCast(
     Type *IntPtrTy ///< Integer type corresponding to pointer

Modified: llvm/trunk/lib/Analysis/Lint.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/Lint.cpp?rev=314763&r1=314762&r2=314763&view=diff
==============================================================================
--- llvm/trunk/lib/Analysis/Lint.cpp (original)
+++ llvm/trunk/lib/Analysis/Lint.cpp Mon Oct  2 23:03:49 2017
@@ -683,7 +683,7 @@ Value *Lint::findValueImpl(Value *V, boo
     if (Instruction::isCast(CE->getOpcode())) {
       if (CastInst::isNoopCast(Instruction::CastOps(CE->getOpcode()),
                                CE->getOperand(0)->getType(), CE->getType(),
-                               DL->getIntPtrType(V->getType())))
+                               *DL))
         return findValueImpl(CE->getOperand(0), OffsetOk, Visited);
     } else if (CE->getOpcode() == Instruction::ExtractValue) {
       ArrayRef<unsigned> Indices = CE->getIndices();

Modified: llvm/trunk/lib/IR/Instructions.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/IR/Instructions.cpp?rev=314763&r1=314762&r2=314763&view=diff
==============================================================================
--- llvm/trunk/lib/IR/Instructions.cpp (original)
+++ llvm/trunk/lib/IR/Instructions.cpp Mon Oct  2 23:03:49 2017
@@ -2326,21 +2326,29 @@ bool CastInst::isNoopCast(Instruction::C
 }
 
 /// @brief Determine if a cast is a no-op.
+bool CastInst::isNoopCast(Instruction::CastOps Opcode,
+                          Type *SrcTy,
+                          Type *DestTy,
+                          const DataLayout &DL) {
+  Type *PtrOpTy = nullptr;
+  if (Opcode == Instruction::PtrToInt)
+    PtrOpTy = SrcTy;
+  else if (Opcode == Instruction::IntToPtr)
+    PtrOpTy = DestTy;
+
+  Type *IntPtrTy = PtrOpTy ? DL.getIntPtrType(PtrOpTy) :
+                             DL.getIntPtrType(SrcTy->getContext(), 0);
+
+  return isNoopCast(Opcode, SrcTy, DestTy, IntPtrTy);
+}
+
+/// @brief Determine if a cast is a no-op.
 bool CastInst::isNoopCast(Type *IntPtrTy) const {
   return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), IntPtrTy);
 }
 
 bool CastInst::isNoopCast(const DataLayout &DL) const {
-  Type *PtrOpTy = nullptr;
-  if (getOpcode() == Instruction::PtrToInt)
-    PtrOpTy = getOperand(0)->getType();
-  else if (getOpcode() == Instruction::IntToPtr)
-    PtrOpTy = getType();
-
-  Type *IntPtrTy =
-      PtrOpTy ? DL.getIntPtrType(PtrOpTy) : DL.getIntPtrType(getContext(), 0);
-
-  return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), IntPtrTy);
+  return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), DL);
 }
 
 /// This function determines if a pair of casts can be eliminated and what

Added: llvm/trunk/test/Analysis/Lint/noop-cast-expr-no-pointer.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Analysis/Lint/noop-cast-expr-no-pointer.ll?rev=314763&view=auto
==============================================================================
--- llvm/trunk/test/Analysis/Lint/noop-cast-expr-no-pointer.ll (added)
+++ llvm/trunk/test/Analysis/Lint/noop-cast-expr-no-pointer.ll Mon Oct  2 23:03:49 2017
@@ -0,0 +1,23 @@
+; RUN: opt -lint < %s
+
+; lint shouldn't crash on any of the below functions
+
+ at g_1 = external global [3 x i32]
+ at g_2 = external global [2 x i32]
+
+define void @test1() {
+entry:
+  tail call void @f1(i16 zext (i1 icmp eq (i32* getelementptr inbounds ([2 x i32], [2 x i32]* @g_2, i64 0, i64 0), i32* getelementptr inbounds ([3 x i32], [3 x i32]* @g_1, i64 0, i64 1)) to i16))
+  ret void
+}
+
+declare void @f1(i16)
+
+define void @test2() {
+  tail call void inttoptr (i64 sext (i32 ptrtoint (void ()* @f2 to i32) to i64) to void ()*)()
+
+  ret void
+}
+
+declare void @f2()
+




More information about the llvm-commits mailing list