Index: lib/Target/ARM/ARMCodeGenPrepare.cpp =================================================================== --- lib/Target/ARM/ARMCodeGenPrepare.cpp +++ lib/Target/ARM/ARMCodeGenPrepare.cpp @@ -114,8 +114,8 @@ SmallPtrSet Promoted; Module *M = nullptr; LLVMContext &Ctx; - Type *ExtTy = nullptr; - Type *OrigTy = nullptr; + IntegerType *ExtTy = nullptr; + IntegerType *OrigTy = nullptr; void PrepareConstants(SmallPtrSetImpl &Visited, SmallPtrSetImpl &SafeToPromote); @@ -139,6 +139,8 @@ } InstsToRemove.clear(); NewInsts.clear(); + TruncTysMap.clear(); + Promoted.clear(); } void Mutate(Type *OrigTy, @@ -514,21 +516,24 @@ IRBuilder<> Builder{Ctx}; auto InsertZExt = [&](Value *V, Instruction *InsertPt) { + assert(V->getType() != ExtTy && "zext already extends to i32"); LLVM_DEBUG(dbgs() << "ARM CGP: Inserting ZExt for " << *V << "\n"); Builder.SetInsertPoint(InsertPt); if (auto *I = dyn_cast(V)) Builder.SetCurrentDebugLocation(I->getDebugLoc()); - auto *ZExt = cast(Builder.CreateZExt(V, ExtTy)); - if (isa(V)) - ZExt->moveBefore(InsertPt); - else - ZExt->moveAfter(InsertPt); + + Value *ZExt = Builder.CreateZExt(V, ExtTy); + if (auto *I = dyn_cast(ZExt)) { + if (isa(V)) + I->moveBefore(InsertPt); + else + I->moveAfter(InsertPt); + NewInsts.insert(I); + } ReplaceAllUsersOfWith(V, ZExt); - NewInsts.insert(ZExt); TruncTysMap[ZExt] = TruncTysMap[V]; }; - // Now, insert extending instructions between the sources and their users. LLVM_DEBUG(dbgs() << "ARM CGP: Promoting sources:\n"); for (auto V : Sources) { @@ -673,7 +678,11 @@ SmallPtrSetImpl &SafeToPromote) { LLVM_DEBUG(dbgs() << "ARM CGP: Promoting use-def chains to from " << ARMCodeGenPrepare::TypeSize << " to 32-bits\n"); - this->OrigTy = OrigTy; + + assert(isa(OrigTy) && "expected integer type"); + this->OrigTy = cast(OrigTy); + assert(OrigTy->getPrimitiveSizeInBits() < ExtTy->getPrimitiveSizeInBits() && + "original type not smaller than extended type"); // Cache original types. for (auto *V : Visited)