Index: llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp =================================================================== --- llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp +++ llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp @@ -2319,9 +2319,12 @@ if (auto *C = dyn_cast(V)) { NewV = ConstantExpr::getBitCast(C, DestTy); } else if (auto *LI = dyn_cast(V)) { - Builder.SetInsertPoint(LI->getNextNode()); - NewV = Builder.CreateBitCast(LI, DestTy); - Worklist.Add(LI); + // Explicitly perform load combine to make sure no opposing transform + // can remove the bitcast in the meantime and trigger an infinite loop. + Builder.SetInsertPoint(LI); + NewV = combineLoadToNewType(*LI, DestTy); + replaceInstUsesWith(*LI, UndefValue::get(LI->getType())); + eraseInstFromFunction(*LI); } else if (auto *BCI = dyn_cast(V)) { NewV = BCI->getOperand(0); } else if (auto *PrevPN = dyn_cast(V)) { Index: llvm/lib/Transforms/InstCombine/InstCombineInternal.h =================================================================== --- llvm/lib/Transforms/InstCombine/InstCombineInternal.h +++ llvm/lib/Transforms/InstCombine/InstCombineInternal.h @@ -467,6 +467,9 @@ /// \return true if successful. bool replacePointer(Instruction &I, Value *V); + LoadInst *combineLoadToNewType(LoadInst &LI, Type *NewTy, + const Twine &Suffix = ""); + private: bool shouldChangeType(unsigned FromBitWidth, unsigned ToBitWidth) const; bool shouldChangeType(Type *From, Type *To) const; Index: llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp =================================================================== --- llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp +++ llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp @@ -449,8 +449,8 @@ /// /// Note that this will create all of the instructions with whatever insert /// point the \c InstCombiner currently is using. -static LoadInst *combineLoadToNewType(InstCombiner &IC, LoadInst &LI, Type *NewTy, - const Twine &Suffix = "") { +LoadInst *InstCombiner::combineLoadToNewType(LoadInst &LI, Type *NewTy, + const Twine &Suffix) { assert((!LI.isAtomic() || isSupportedAtomicType(NewTy)) && "can't fold an atomic load to requested type"); @@ -460,9 +460,9 @@ if (!(match(Ptr, m_BitCast(m_Value(NewPtr))) && NewPtr->getType()->getPointerElementType() == NewTy && NewPtr->getType()->getPointerAddressSpace() == AS)) - NewPtr = IC.Builder.CreateBitCast(Ptr, NewTy->getPointerTo(AS)); + NewPtr = Builder.CreateBitCast(Ptr, NewTy->getPointerTo(AS)); - LoadInst *NewLoad = IC.Builder.CreateAlignedLoad( + LoadInst *NewLoad = Builder.CreateAlignedLoad( NewTy, NewPtr, LI.getAlignment(), LI.isVolatile(), LI.getName() + Suffix); NewLoad->setAtomic(LI.getOrdering(), LI.getSyncScopeID()); copyMetadataForLoad(*NewLoad, LI); @@ -600,9 +600,8 @@ return SI && SI->getPointerOperand() != &LI && !SI->getPointerOperand()->isSwiftError(); })) { - LoadInst *NewLoad = combineLoadToNewType( - IC, LI, - Type::getIntNTy(LI.getContext(), DL.getTypeStoreSizeInBits(Ty))); + LoadInst *NewLoad = IC.combineLoadToNewType( + LI, Type::getIntNTy(LI.getContext(), DL.getTypeStoreSizeInBits(Ty))); // Replace all the stores with stores of the newly loaded value. for (auto UI = LI.user_begin(), UE = LI.user_end(); UI != UE;) { auto *SI = cast(*UI++); @@ -624,7 +623,7 @@ if (auto* CI = dyn_cast(LI.user_back())) if (CI->isNoopCast(DL)) if (!LI.isAtomic() || isSupportedAtomicType(CI->getDestTy())) { - LoadInst *NewLoad = combineLoadToNewType(IC, LI, CI->getDestTy()); + LoadInst *NewLoad = IC.combineLoadToNewType(LI, CI->getDestTy()); CI->replaceAllUsesWith(NewLoad); IC.eraseInstFromFunction(*CI); return &LI; @@ -652,8 +651,8 @@ // If the struct only have one element, we unpack. auto NumElements = ST->getNumElements(); if (NumElements == 1) { - LoadInst *NewLoad = combineLoadToNewType(IC, LI, ST->getTypeAtIndex(0U), - ".unpack"); + LoadInst *NewLoad = IC.combineLoadToNewType(LI, ST->getTypeAtIndex(0U), + ".unpack"); AAMDNodes AAMD; LI.getAAMetadata(AAMD); NewLoad->setAAMetadata(AAMD); @@ -702,7 +701,7 @@ auto *ET = AT->getElementType(); auto NumElements = AT->getNumElements(); if (NumElements == 1) { - LoadInst *NewLoad = combineLoadToNewType(IC, LI, ET, ".unpack"); + LoadInst *NewLoad = IC.combineLoadToNewType(LI, ET, ".unpack"); AAMDNodes AAMD; LI.getAAMetadata(AAMD); NewLoad->setAAMetadata(AAMD); @@ -1345,7 +1344,7 @@ return false; IC.Builder.SetInsertPoint(LI); - LoadInst *NewLI = combineLoadToNewType(IC, *LI, CmpLoadTy); + LoadInst *NewLI = IC.combineLoadToNewType(*LI, CmpLoadTy); // Replace all the stores with stores of the newly loaded value. for (auto *UI : LI->users()) { auto *USI = cast(UI); Index: llvm/test/Transforms/InstCombine/pr44245.ll =================================================================== --- /dev/null +++ llvm/test/Transforms/InstCombine/pr44245.ll @@ -0,0 +1,176 @@ +; NOTE: Assertions have been autogenerated by utils/update_test_checks.py +; RUN: opt -S -instcombine -instcombine-infinite-loop-threshold=2 < %s | FileCheck %s + +; This used to cause on infinite instcombine loop. + +define void @test(i1 %c) { +; CHECK-LABEL: @test( +; CHECK-NEXT: bb16: +; CHECK-NEXT: br i1 [[C:%.*]], label [[BB17:%.*]], label [[BB24:%.*]] +; CHECK: bb17: +; CHECK-NEXT: [[TMP0:%.*]] = phi i8* [ [[TMP1:%.*]], [[BB47:%.*]] ], [ undef, [[BB16:%.*]] ] +; CHECK-NEXT: store i8* [[TMP0]], i8** undef, align 8 +; CHECK-NEXT: ret void +; CHECK: bb24: +; CHECK-NEXT: br i1 [[C]], label [[BB44:%.*]], label [[BB49:%.*]] +; CHECK: bb44: +; CHECK-NEXT: [[TMP469:%.*]] = load i8*, i8** inttoptr (i64 16 to i8**), align 16 +; CHECK-NEXT: br label [[BB47]] +; CHECK: bb47: +; CHECK-NEXT: [[TMP1]] = phi i8* [ [[TMP2:%.*]], [[BB150:%.*]] ], [ [[TMP1223:%.*]], [[BB119:%.*]] ], [ [[TMP1034:%.*]], [[BB101:%.*]] ], [ [[TMP935:%.*]], [[BB91:%.*]] ], [ [[TMP836:%.*]], [[BB81:%.*]] ], [ [[TMP707:%.*]], [[BB67:%.*]] ], [ [[TMP588:%.*]], [[BB56:%.*]] ], [ [[TMP469]], [[BB44]] ] +; CHECK-NEXT: br label [[BB17]] +; CHECK: bb49: +; CHECK-NEXT: br i1 [[C]], label [[BB56]], label [[BB59:%.*]] +; CHECK: bb56: +; CHECK-NEXT: [[TMP588]] = load i8*, i8** inttoptr (i64 16 to i8**), align 16 +; CHECK-NEXT: br label [[BB47]] +; CHECK: bb59: +; CHECK-NEXT: br i1 [[C]], label [[BB67]], label [[BB71:%.*]] +; CHECK: bb67: +; CHECK-NEXT: [[TMP707]] = load i8*, i8** inttoptr (i64 16 to i8**), align 16 +; CHECK-NEXT: br label [[BB47]] +; CHECK: bb71: +; CHECK-NEXT: br i1 [[C]], label [[BB81]], label [[BB84:%.*]] +; CHECK: bb81: +; CHECK-NEXT: [[TMP836]] = load i8*, i8** inttoptr (i64 16 to i8**), align 16 +; CHECK-NEXT: br label [[BB47]] +; CHECK: bb84: +; CHECK-NEXT: br i1 [[C]], label [[BB91]], label [[BB94:%.*]] +; CHECK: bb91: +; CHECK-NEXT: [[TMP935]] = load i8*, i8** inttoptr (i64 16 to i8**), align 16 +; CHECK-NEXT: br label [[BB47]] +; CHECK: bb94: +; CHECK-NEXT: br i1 [[C]], label [[BB101]], label [[BB104:%.*]] +; CHECK: bb101: +; CHECK-NEXT: [[TMP1034]] = load i8*, i8** inttoptr (i64 16 to i8**), align 16 +; CHECK-NEXT: br label [[BB47]] +; CHECK: bb104: +; CHECK-NEXT: br i1 [[C]], label [[BB119]], label [[BB123:%.*]] +; CHECK: bb119: +; CHECK-NEXT: [[TMP1223]] = load i8*, i8** inttoptr (i64 16 to i8**), align 16 +; CHECK-NEXT: br label [[BB47]] +; CHECK: bb123: +; CHECK-NEXT: br i1 [[C]], label [[BB147:%.*]], label [[BB152:%.*]] +; CHECK: bb147: +; CHECK-NEXT: [[TMP1492:%.*]] = load i8*, i8** inttoptr (i64 16 to i8**), align 16 +; CHECK-NEXT: br label [[BB150]] +; CHECK: bb150: +; CHECK-NEXT: [[TMP2]] = phi i8* [ [[TMP1841:%.*]], [[BB152]] ], [ [[TMP1492]], [[BB147]] ] +; CHECK-NEXT: br label [[BB47]] +; CHECK: bb152: +; CHECK-NEXT: [[TMP1841]] = load i8*, i8** inttoptr (i64 16 to i8**), align 16 +; CHECK-NEXT: call void undef() +; CHECK-NEXT: br label [[BB150]] +; +bb16: ; preds = %bb + br i1 %c, label %bb17, label %bb24 + +bb17: ; preds = %bb47, %bb17 + %0 = phi i8* [ %1, %bb47 ], [ undef, %bb16 ] + store i8* %0, i8** undef, align 8 + ret void + +bb24: ; preds = %bb24 + br i1 %c, label %bb44, label %bb49 + +bb44: ; preds = %bb43 + %tmp46 = load i64*, i64** inttoptr (i64 16 to i64**), align 16 + br label %bb47 + +bb47: ; preds = %bb150, %bb119, %bb101, %bb91, %bb81, %bb67, %bb56, %bb44 + %.in1 = phi i64* [ %.in, %bb150 ], [ %tmp122, %bb119 ], [ %tmp103, %bb101 ], [ %tmp93, %bb91 ], [ %tmp83, %bb81 ], [ %tmp70, %bb67 ], [ %tmp58, %bb56 ], [ %tmp46, %bb44 ] + %1 = bitcast i64* %.in1 to i8* + br label %bb17 + +bb49: ; preds = %bb49 + br i1 %c, label %bb56, label %bb59 + +bb56: ; preds = %bb55 + %tmp58 = load i64*, i64** inttoptr (i64 16 to i64**), align 16 + br label %bb47 + +bb59: ; preds = %bb59 + br i1 %c, label %bb67, label %bb71 + +bb67: ; preds = %bb66 + %tmp70 = load i64*, i64** inttoptr (i64 16 to i64**), align 16 + br label %bb47 + +bb71: ; preds = %bb71 + br i1 %c, label %bb81, label %bb84 + +bb81: ; preds = %bb80 + %tmp83 = load i64*, i64** inttoptr (i64 16 to i64**), align 16 + br label %bb47 + +bb84: ; preds = %bb84 + br i1 %c, label %bb91, label %bb94 + +bb91: ; preds = %bb90 + %tmp93 = load i64*, i64** inttoptr (i64 16 to i64**), align 16 + br label %bb47 + +bb94: ; preds = %bb94 + br i1 %c, label %bb101, label %bb104 + +bb101: ; preds = %bb100 + %tmp103 = load i64*, i64** inttoptr (i64 16 to i64**), align 16 + br label %bb47 + +bb104: ; preds = %bb104 + br i1 %c, label %bb119, label %bb123 + +bb119: ; preds = %bb118 + %tmp122 = load i64*, i64** inttoptr (i64 16 to i64**), align 16 + br label %bb47 + +bb123: ; preds = %bb123 + br i1 %c, label %bb147, label %bb152 + +bb147: ; preds = %bb146 + %tmp149 = load i64*, i64** inttoptr (i64 16 to i64**), align 16 + br label %bb150 + +bb150: ; preds = %bb152, %bb147 + %.in = phi i64* [ %tmp184, %bb152 ], [ %tmp149, %bb147 ] + br label %bb47 + +bb152: ; preds = %bb146 + %tmp184 = load i64*, i64** inttoptr (i64 16 to i64**), align 16 + call void undef() + br label %bb150 +} + +; This used to cause an instcombine loop when the problem above was +; addressed in a non-robust fashion. + +%type_1 = type {} +%type_2 = type {} +%type_3 = type {} + +define void @test_2(i1 %c) local_unnamed_addr { +entry: + br label %while.cond + +while.cond: ; preds = %cond.end144, %entry + %link.0 = phi %type_2* [ undef, %entry ], [ %cond145, %cond.end144 ] + %os115 = bitcast %type_2* %link.0 to %type_3* + %ou116 = getelementptr inbounds %type_3, %type_3* %os115, i32 0 + %os1117 = bitcast %type_3* %ou116 to %type_1* + br label %for.cond + +for.cond: ; preds = %while.cond + br i1 %c, label %cond.true133, label %cond.false138 + +cond.true133: ; preds = %sw.epilog + %0 = load %type_2*, %type_2** undef, align 8 + br label %cond.end144 + +cond.false138: ; preds = %sw.epilog + %1 = load %type_2*, %type_2** undef, align 8 + br label %cond.end144 + +cond.end144: ; preds = %cond.false138, %cond.true133 + %cond145 = phi %type_2* [ %0, %cond.true133 ], [ %1, %cond.false138 ] + br label %while.cond +}