diff --git a/llvm/lib/CodeGen/CodeGenPrepare.cpp b/llvm/lib/CodeGen/CodeGenPrepare.cpp index f49d12748f3d4..b9ffb8a8318fb 100644 --- a/llvm/lib/CodeGen/CodeGenPrepare.cpp +++ b/llvm/lib/CodeGen/CodeGenPrepare.cpp @@ -6122,6 +6122,7 @@ bool CodeGenPrepare::optimizePhiType( SmallVector Worklist; Worklist.push_back(cast(I)); SmallPtrSet PhiNodes; + SmallPtrSet Constants; PhiNodes.insert(I); Visited.insert(I); SmallPtrSet Defs; @@ -6164,9 +6165,10 @@ bool CodeGenPrepare::optimizePhiType( AnyAnchored |= !isa(OpBC->getOperand(0)) && !isa(OpBC->getOperand(0)); } - } else if (!isa(V)) { + } else if (auto *OpC = dyn_cast(V)) + Constants.insert(OpC); + else return false; - } } } @@ -6208,7 +6210,8 @@ bool CodeGenPrepare::optimizePhiType( // Create all the new phi nodes of the new type, and bitcast any loads to the // correct type. ValueToValueMap ValMap; - ValMap[UndefValue::get(PhiTy)] = UndefValue::get(ConvertTy); + for (ConstantData *C : Constants) + ValMap[C] = ConstantExpr::getCast(Instruction::BitCast, C, ConvertTy); for (Instruction *D : Defs) { if (isa(D)) { ValMap[D] = D->getOperand(0); diff --git a/llvm/test/CodeGen/AArch64/convertphitype.ll b/llvm/test/CodeGen/AArch64/convertphitype.ll index f6bfb69e510bb..22d130d4000a1 100644 --- a/llvm/test/CodeGen/AArch64/convertphitype.ll +++ b/llvm/test/CodeGen/AArch64/convertphitype.ll @@ -881,11 +881,11 @@ define float @convphi2_zero(i32 *%s, i32 *%d, i32 %n) { ; CHECK-NEXT: br i1 [[CMP15]], label [[THEN:%.*]], label [[END:%.*]] ; CHECK: then: ; CHECK-NEXT: [[LS:%.*]] = load i32, i32* [[S:%.*]], align 4 +; CHECK-NEXT: [[LS_BC:%.*]] = bitcast i32 [[LS]] to float ; CHECK-NEXT: br label [[END]] ; CHECK: end: -; CHECK-NEXT: [[PHI:%.*]] = phi i32 [ [[LS]], [[THEN]] ], [ 0, [[ENTRY:%.*]] ] -; CHECK-NEXT: [[B:%.*]] = bitcast i32 [[PHI]] to float -; CHECK-NEXT: ret float [[B]] +; CHECK-NEXT: [[PHI_TC:%.*]] = phi float [ [[LS_BC]], [[THEN]] ], [ 0.000000e+00, [[ENTRY:%.*]] ] +; CHECK-NEXT: ret float [[PHI_TC]] ; entry: %cmp15 = icmp sgt i32 %n, 0 @@ -908,11 +908,11 @@ define i32 @convphi2f_zero(float *%s, float *%d, i32 %n) { ; CHECK-NEXT: br i1 [[CMP15]], label [[THEN:%.*]], label [[END:%.*]] ; CHECK: then: ; CHECK-NEXT: [[LS:%.*]] = load float, float* [[S:%.*]], align 4 +; CHECK-NEXT: [[LS_BC:%.*]] = bitcast float [[LS]] to i32 ; CHECK-NEXT: br label [[END]] ; CHECK: end: -; CHECK-NEXT: [[PHI:%.*]] = phi float [ [[LS]], [[THEN]] ], [ 0.000000e+00, [[ENTRY:%.*]] ] -; CHECK-NEXT: [[B:%.*]] = bitcast float [[PHI]] to i32 -; CHECK-NEXT: ret i32 [[B]] +; CHECK-NEXT: [[PHI_TC:%.*]] = phi i32 [ [[LS_BC]], [[THEN]] ], [ 0, [[ENTRY:%.*]] ] +; CHECK-NEXT: ret i32 [[PHI_TC]] ; entry: %cmp15 = icmp sgt i32 %n, 0 @@ -935,11 +935,11 @@ define float @convphi2_ten(i32 *%s, i32 *%d, i32 %n) { ; CHECK-NEXT: br i1 [[CMP15]], label [[THEN:%.*]], label [[END:%.*]] ; CHECK: then: ; CHECK-NEXT: [[LS:%.*]] = load i32, i32* [[S:%.*]], align 4 +; CHECK-NEXT: [[LS_BC:%.*]] = bitcast i32 [[LS]] to float ; CHECK-NEXT: br label [[END]] ; CHECK: end: -; CHECK-NEXT: [[PHI:%.*]] = phi i32 [ [[LS]], [[THEN]] ], [ 10, [[ENTRY:%.*]] ] -; CHECK-NEXT: [[B:%.*]] = bitcast i32 [[PHI]] to float -; CHECK-NEXT: ret float [[B]] +; CHECK-NEXT: [[PHI_TC:%.*]] = phi float [ [[LS_BC]], [[THEN]] ], [ 0x36D4000000000000, [[ENTRY:%.*]] ] +; CHECK-NEXT: ret float [[PHI_TC]] ; entry: %cmp15 = icmp sgt i32 %n, 0 @@ -962,11 +962,11 @@ define i32 @convphi2f_ten(float *%s, float *%d, i32 %n) { ; CHECK-NEXT: br i1 [[CMP15]], label [[THEN:%.*]], label [[END:%.*]] ; CHECK: then: ; CHECK-NEXT: [[LS:%.*]] = load float, float* [[S:%.*]], align 4 +; CHECK-NEXT: [[LS_BC:%.*]] = bitcast float [[LS]] to i32 ; CHECK-NEXT: br label [[END]] ; CHECK: end: -; CHECK-NEXT: [[PHI:%.*]] = phi float [ [[LS]], [[THEN]] ], [ 1.000000e+01, [[ENTRY:%.*]] ] -; CHECK-NEXT: [[B:%.*]] = bitcast float [[PHI]] to i32 -; CHECK-NEXT: ret i32 [[B]] +; CHECK-NEXT: [[PHI_TC:%.*]] = phi i32 [ [[LS_BC]], [[THEN]] ], [ 1092616192, [[ENTRY:%.*]] ] +; CHECK-NEXT: ret i32 [[PHI_TC]] ; entry: %cmp15 = icmp sgt i32 %n, 0