Index: llvm/trunk/include/llvm/IR/InstrTypes.h =================================================================== --- llvm/trunk/include/llvm/IR/InstrTypes.h +++ llvm/trunk/include/llvm/IR/InstrTypes.h @@ -789,6 +789,14 @@ 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 Index: llvm/trunk/lib/Analysis/Lint.cpp =================================================================== --- llvm/trunk/lib/Analysis/Lint.cpp +++ llvm/trunk/lib/Analysis/Lint.cpp @@ -683,7 +683,7 @@ 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 Indices = CE->getIndices(); Index: llvm/trunk/lib/IR/Instructions.cpp =================================================================== --- llvm/trunk/lib/IR/Instructions.cpp +++ llvm/trunk/lib/IR/Instructions.cpp @@ -2326,21 +2326,29 @@ } /// @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 Index: llvm/trunk/test/Analysis/Lint/noop-cast-expr-no-pointer.ll =================================================================== --- llvm/trunk/test/Analysis/Lint/noop-cast-expr-no-pointer.ll +++ llvm/trunk/test/Analysis/Lint/noop-cast-expr-no-pointer.ll @@ -0,0 +1,23 @@ +; RUN: opt -lint < %s + +; lint shouldn't crash on any of the below functions + +@g_1 = external global [3 x i32] +@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() +