diff --git a/include/llvm/Analysis/MemoryBuiltins.h b/include/llvm/Analysis/MemoryBuiltins.h index dd2fce41359..cc1a8823600 100644 --- a/include/llvm/Analysis/MemoryBuiltins.h +++ b/include/llvm/Analysis/MemoryBuiltins.h @@ -161,12 +161,14 @@ typedef std::pair SizeOffsetType; class ObjectSizeOffsetVisitor : public InstVisitor { + typedef DenseMap CacheMapTy; + const DataLayout *TD; const TargetLibraryInfo *TLI; bool RoundToAlign; unsigned IntTyBits; APInt Zero; - SmallPtrSet SeenInsts; + CacheMapTy CacheMap; APInt align(APInt Size, uint64_t Align); diff --git a/lib/Analysis/MemoryBuiltins.cpp b/lib/Analysis/MemoryBuiltins.cpp index 90f5811289c..bd7758fe068 100644 --- a/lib/Analysis/MemoryBuiltins.cpp +++ b/lib/Analysis/MemoryBuiltins.cpp @@ -405,16 +405,23 @@ ObjectSizeOffsetVisitor::ObjectSizeOffsetVisitor(const DataLayout *TD, SizeOffsetType ObjectSizeOffsetVisitor::compute(Value *V) { V = V->stripPointerCasts(); - if (Instruction *I = dyn_cast(V)) { - // If we have already seen this instruction, bail out. Cycles can happen in - // unreachable code after constant propagation. - if (!SeenInsts.insert(I)) - return unknown(); + if (isa(V) || isa(V)) { + // Return cached value or insert unknown in cache if size of V was not + // computed yet in order to avoid recursions in PHis. + std::pair CacheVal = + CacheMap.insert(std::make_pair(V, unknown())); + if (!CacheVal.second) + return CacheVal.first->second; + + SizeOffsetType Result; if (GEPOperator *GEP = dyn_cast(V)) - return visitGEPOperator(*GEP); - return visit(*I); + Result = visitGEPOperator(*GEP); + else + Result = visit(cast(*V)); + return CacheMap[V] = Result; } + if (Argument *A = dyn_cast(V)) return visitArgument(*A); if (ConstantPointerNull *P = dyn_cast(V)) @@ -428,8 +435,6 @@ SizeOffsetType ObjectSizeOffsetVisitor::compute(Value *V) { if (ConstantExpr *CE = dyn_cast(V)) { if (CE->getOpcode() == Instruction::IntToPtr) return unknown(); // clueless - if (CE->getOpcode() == Instruction::GetElementPtr) - return visitGEPOperator(cast(*CE)); } DEBUG(dbgs() << "ObjectSizeOffsetVisitor::compute() unhandled value: " << *V @@ -563,9 +568,21 @@ SizeOffsetType ObjectSizeOffsetVisitor::visitLoadInst(LoadInst&) { return unknown(); } -SizeOffsetType ObjectSizeOffsetVisitor::visitPHINode(PHINode&) { - // too complex to analyze statically. - return unknown(); +SizeOffsetType ObjectSizeOffsetVisitor::visitPHINode(PHINode &PHI) { + if (PHI.getNumIncomingValues() == 0) + return unknown(); + + SizeOffsetType Ret = compute(PHI.getIncomingValue(0)); + if (!bothKnown(Ret)) + return unknown(); + + // Verify that all PHI incoming pointers have the same size and offset. + for (unsigned i = 1, e = PHI.getNumIncomingValues(); i != e; ++i) { + SizeOffsetType EdgeData = compute(PHI.getIncomingValue(i)); + if (!bothKnown(EdgeData) || EdgeData != Ret) + return unknown(); + } + return Ret; } SizeOffsetType ObjectSizeOffsetVisitor::visitSelectInst(SelectInst &I) { diff --git a/test/Transforms/InstCombine/objsize.ll b/test/Transforms/InstCombine/objsize.ll index 31a3cb46e45..0ead9d12374 100644 --- a/test/Transforms/InstCombine/objsize.ll +++ b/test/Transforms/InstCombine/objsize.ll @@ -256,3 +256,131 @@ xpto: return: ret i32 7 } + +declare noalias i8* @valloc(i32) nounwind + +; CHECK: @test14 +; CHECK: ret i32 6 +define i32 @test14(i32 %a) nounwind { + switch i32 %a, label %sw.default [ + i32 1, label %sw.bb + i32 2, label %sw.bb1 + ] + +sw.bb: + %call = tail call noalias i8* @malloc(i32 6) nounwind + br label %sw.epilog + +sw.bb1: + %call2 = tail call noalias i8* @calloc(i32 3, i32 2) nounwind + br label %sw.epilog + +sw.default: + %call3 = tail call noalias i8* @valloc(i32 6) nounwind + br label %sw.epilog + +sw.epilog: + %b.0 = phi i8* [ %call3, %sw.default ], [ %call2, %sw.bb1 ], [ %call, %sw.bb ] + %1 = tail call i32 @llvm.objectsize.i32(i8* %b.0, i1 false) + ret i32 %1 +} + +; CHECK: @test15 +; CHECK: llvm.objectsize +define i32 @test15(i32 %a) nounwind { + switch i32 %a, label %sw.default [ + i32 1, label %sw.bb + i32 2, label %sw.bb1 + ] + +sw.bb: + %call = tail call noalias i8* @malloc(i32 3) nounwind + br label %sw.epilog + +sw.bb1: + %call2 = tail call noalias i8* @calloc(i32 2, i32 1) nounwind + br label %sw.epilog + +sw.default: + %call3 = tail call noalias i8* @valloc(i32 3) nounwind + br label %sw.epilog + +sw.epilog: + %b.0 = phi i8* [ %call3, %sw.default ], [ %call2, %sw.bb1 ], [ %call, %sw.bb ] + %1 = tail call i32 @llvm.objectsize.i32(i8* %b.0, i1 false) + ret i32 %1 +} + +; CHECK: @test16 +; CHECK: llvm.objectsize +define i32 @test16(i8* %a, i32 %n) nounwind { + %b = alloca [5 x i8], align 1 + %c = alloca [5 x i8], align 1 + switch i32 %n, label %sw.default [ + i32 1, label %sw.bb + i32 2, label %sw.bb1 + ] + +sw.bb: + %bp = bitcast [5 x i8]* %b to i8* + br label %sw.epilog + +sw.bb1: + %cp = bitcast [5 x i8]* %c to i8* + br label %sw.epilog + +sw.default: + br label %sw.epilog + +sw.epilog: + %phi = phi i8* [ %a, %sw.default ], [ %cp, %sw.bb1 ], [ %bp, %sw.bb ] + %sz = call i32 @llvm.objectsize.i32(i8* %phi, i1 false) + ret i32 %sz +} + +; CHECK: @test17 +; CHECK: ret i32 5 +define i32 @test17(i32 %n) nounwind { + %b = alloca [5 x i8], align 1 + %c = alloca [5 x i8], align 1 + %bp = bitcast [5 x i8]* %b to i8* + switch i32 %n, label %sw.default [ + i32 1, label %sw.bb + i32 2, label %sw.bb1 + ] + +sw.bb: + br label %sw.epilog + +sw.bb1: + %cp = bitcast [5 x i8]* %c to i8* + br label %sw.epilog + +sw.default: + br label %sw.epilog + +sw.epilog: + %phi = phi i8* [ %bp, %sw.default ], [ %cp, %sw.bb1 ], [ %bp, %sw.bb ] + %sz = call i32 @llvm.objectsize.i32(i8* %phi, i1 false) + ret i32 %sz +} + +@globalalias = alias internal [60 x i8]* @a + +; CHECK: @test18 +; CHECK-NEXT: ret i32 60 +define i32 @test18() { + %bc = bitcast [60 x i8]* @globalalias to i8* + %1 = call i32 @llvm.objectsize.i32(i8* %bc, i1 false) + ret i32 %1 +} + +@globalalias2 = alias weak [60 x i8]* @a + +; CHECK: @test19 +; CHECK: llvm.objectsize +define i32 @test19() { + %bc = bitcast [60 x i8]* @globalalias2 to i8* + %1 = call i32 @llvm.objectsize.i32(i8* %bc, i1 false) + ret i32 %1 +}