Index: llvm/lib/CodeGen/TypePromotion.cpp =================================================================== --- llvm/lib/CodeGen/TypePromotion.cpp +++ llvm/lib/CodeGen/TypePromotion.cpp @@ -103,7 +103,6 @@ namespace { class IRPromoter { LLVMContext &Ctx; - IntegerType *OrigTy = nullptr; unsigned PromotedWidth = 0; SetVector &Visited; SetVector &Sources; @@ -123,16 +122,13 @@ void Cleanup(); public: - IRPromoter(LLVMContext &C, IntegerType *Ty, unsigned Width, + IRPromoter(LLVMContext &C, unsigned Width, SetVector &visited, SetVector &sources, SetVector &sinks, SmallPtrSetImpl &wrap) - : Ctx(C), OrigTy(Ty), PromotedWidth(Width), Visited(visited), + : Ctx(C), PromotedWidth(Width), Visited(visited), Sources(sources), Sinks(sinks), SafeWrap(wrap) { ExtTy = IntegerType::get(Ctx, PromotedWidth); - assert(OrigTy->getPrimitiveSizeInBits().getFixedSize() < - ExtTy->getPrimitiveSizeInBits().getFixedSize() && - "Original type not smaller than extended type"); } void Mutate(); @@ -586,11 +582,9 @@ continue; } - // Unless they produce a value that is narrower than ExtTy, we can - // replace the result of the zext with the input of a newly inserted - // trunc. - if (NewInsts.count(Src) && isa(Src) && - Src->getType() == OrigTy) { + // We've inserted a trunc for a zext sink, but we already know that the + // input is in range, negating the need for the trunc. + if (NewInsts.count(Src) && isa(Src)) { auto *Trunc = cast(Src); assert(Trunc->getOperand(0)->getType() == ExtTy && "expected inserted trunc to be operating on i32"); @@ -631,9 +625,6 @@ } void IRPromoter::Mutate() { - LLVM_DEBUG(dbgs() << "IR Promotion: Promoting use-def chains from " - << OrigTy->getBitWidth() << " to " << PromotedWidth << "-bits\n"); - // Cache original types of the values that will likely need truncating for (auto *I : Sinks) { if (auto *Call = dyn_cast(I)) { @@ -873,8 +864,11 @@ if (ToPromote < 2 || (Blocks.size() == 1 && (NonFreeArgs > SafeWrap.size()))) return false; - IRPromoter Promoter(*Ctx, cast(OrigTy), PromotedWidth, - CurrentVisited, Sources, Sinks, SafeWrap); + IRPromoter Promoter(*Ctx, PromotedWidth, CurrentVisited, Sources, Sinks, + SafeWrap); + LLVM_DEBUG(dbgs() << "Promoting use-def chains from " + << cast(OrigTy)->getBitWidth() + << " to " << PromotedWidth << "-bits\n"); Promoter.Mutate(); return true; } Index: llvm/test/Transforms/TypePromotion/ARM/casts.ll =================================================================== --- llvm/test/Transforms/TypePromotion/ARM/casts.ll +++ llvm/test/Transforms/TypePromotion/ARM/casts.ll @@ -1116,8 +1116,7 @@ ; CHECK-NEXT: [[TMP5:%.*]] = trunc i32 [[OR]] to i8 ; CHECK-NEXT: store i8 [[TMP5]], i8* [[B]], align 1 ; CHECK-NEXT: [[TMP6:%.*]] = trunc i32 [[OR]] to i8 -; CHECK-NEXT: [[CONV5:%.*]] = zext i8 [[TMP6]] to i32 -; CHECK-NEXT: ret i32 [[CONV5]] +; CHECK-NEXT: ret i32 [[OR]] ; entry: %0 = load i16, i16* %a, align 2 @@ -1143,10 +1142,9 @@ ; CHECK-NEXT: [[TMP3:%.*]] = and i32 [[TMP2]], 255 ; CHECK-NEXT: [[TMP4:%.*]] = udiv i32 [[TMP3]], 3 ; CHECK-NEXT: [[TMP5:%.*]] = trunc i32 [[TMP4]] to i8 -; CHECK-NEXT: [[PHITMP:%.*]] = zext i8 [[TMP5]] to i32 ; CHECK-NEXT: br label [[COND_END]] ; CHECK: cond.end: -; CHECK-NEXT: [[COND:%.*]] = phi i32 [ [[PHITMP]], [[COND_FALSE]] ], [ 0, [[ENTRY:%.*]] ] +; CHECK-NEXT: [[COND:%.*]] = phi i32 [ [[TMP4]], [[COND_FALSE]] ], [ 0, [[ENTRY:%.*]] ] ; CHECK-NEXT: ret i32 [[COND]] ; entry: