Index: lib/Analysis/ConstantFolding.cpp =================================================================== --- lib/Analysis/ConstantFolding.cpp +++ lib/Analysis/ConstantFolding.cpp @@ -1103,11 +1103,17 @@ } if (auto *CE = dyn_cast(C)) { + Constant *Res; if (CE->isCompare()) - return ConstantFoldCompareInstOperands(CE->getPredicate(), Ops[0], Ops[1], - DL, TLI); + Res = ConstantFoldCompareInstOperands(CE->getPredicate(), Ops[0], Ops[1], + DL, TLI); + else + Res = ConstantFoldInstOperandsImpl(CE, CE->getOpcode(), Ops, DL, TLI); - return ConstantFoldInstOperandsImpl(CE, CE->getOpcode(), Ops, DL, TLI); + // Ensure the constant is fully folded. + if (Res != C) + Res = ConstantFoldConstantImpl(Res, DL, TLI, FoldedOps); + return Res; } assert(isa(C)); @@ -1161,27 +1167,27 @@ Ops.push_back(Op); } + Constant *Res; if (const auto *CI = dyn_cast(I)) - return ConstantFoldCompareInstOperands(CI->getPredicate(), Ops[0], Ops[1], - DL, TLI); - - if (const auto *LI = dyn_cast(I)) - return ConstantFoldLoadInst(LI, DL); - - if (auto *IVI = dyn_cast(I)) { - return ConstantExpr::getInsertValue( - cast(IVI->getAggregateOperand()), - cast(IVI->getInsertedValueOperand()), - IVI->getIndices()); - } - - if (auto *EVI = dyn_cast(I)) { - return ConstantExpr::getExtractValue( - cast(EVI->getAggregateOperand()), - EVI->getIndices()); - } - - return ConstantFoldInstOperands(I, Ops, DL, TLI); + Res = ConstantFoldCompareInstOperands(CI->getPredicate(), Ops[0], Ops[1], + DL, TLI); + else if (const auto *LI = dyn_cast(I)) + Res = ConstantFoldLoadInst(LI, DL); + else if (auto *IVI = dyn_cast(I)) + Res = ConstantExpr::getInsertValue( + cast(IVI->getAggregateOperand()), + cast(IVI->getInsertedValueOperand()), + IVI->getIndices()); + else if (auto *EVI = dyn_cast(I)) + Res = ConstantExpr::getExtractValue( + cast(EVI->getAggregateOperand()), EVI->getIndices()); + else + Res = ConstantFoldInstOperands(I, Ops, DL, TLI); + + // Ensure the constant is fully folded. + if (Res) + return ConstantFoldConstantImpl(Res, DL, TLI, FoldedOps); + return nullptr; } Constant *llvm::ConstantFoldConstant(const Constant *C, const DataLayout &DL, Index: test/Analysis/ConstantFolding/gep-zeroinit-vector.ll =================================================================== --- test/Analysis/ConstantFolding/gep-zeroinit-vector.ll +++ test/Analysis/ConstantFolding/gep-zeroinit-vector.ll @@ -9,7 +9,7 @@ define <2 x i16*> @test_gep() { ; CHECK-LABEL: @test_gep( -; CHECK-NEXT: ret <2 x i16*> +; CHECK-NEXT: ret <2 x i16*> ; %A = getelementptr [1 x %rec8], [1 x %rec8]* @a, <2 x i16> zeroinitializer, <2 x i64> zeroinitializer %B = bitcast <2 x %rec8*> %A to <2 x i16*> Index: test/Transforms/ConstProp/bitcast.ll =================================================================== --- test/Transforms/ConstProp/bitcast.ll +++ test/Transforms/ConstProp/bitcast.ll @@ -77,7 +77,7 @@ define i8* @bitcast_to_gep() { ; CHECK-LABEL: @bitcast_to_gep( -; CHECK-NEXT: ret i8* getelementptr inbounds (%T, %T* @G, i32 0, i32 0) +; CHECK-NEXT: ret i8* getelementptr inbounds (%T, %T* @G, i64 0, i32 0) ; %p = bitcast %T* @G to i8* ret i8* %p @@ -85,7 +85,7 @@ define i8 addrspace(1)* @addrspacecast_to_gep() { ; CHECK-LABEL: @addrspacecast_to_gep( -; CHECK-NEXT: ret i8 addrspace(1)* addrspacecast (i8* getelementptr inbounds (%T, %T* @G, i32 0, i32 0) to i8 addrspace(1)*) +; CHECK-NEXT: ret i8 addrspace(1)* addrspacecast (i8* getelementptr inbounds (%T, %T* @G, i64 0, i32 0) to i8 addrspace(1)*) ; %p = addrspacecast %T* @G to i8 addrspace(1)* ret i8 addrspace(1)* %p