[CodeGenPrep] Handle constants in ConvertPhiType

This is a simple addition to the convertPhiTypes in CodeGenPrepare to
consider and convert constants as it converts the phi type. Someone
fixed the bug in the motivating example, so the undef is now a constant
0. This does mean converting between integer and floating point
constants, which may have different materialization.

Differential Revision: https://reviews.llvm.org/D135561
This commit is contained in:
David Green 2022-10-13 16:41:44 +01:00
parent d8cab3f407
commit 16e4e4ab87
2 changed files with 18 additions and 15 deletions

View File

@ -6122,6 +6122,7 @@ bool CodeGenPrepare::optimizePhiType(
SmallVector<Instruction *, 4> Worklist;
Worklist.push_back(cast<Instruction>(I));
SmallPtrSet<PHINode *, 4> PhiNodes;
SmallPtrSet<ConstantData *, 4> Constants;
PhiNodes.insert(I);
Visited.insert(I);
SmallPtrSet<Instruction *, 4> Defs;
@ -6164,9 +6165,10 @@ bool CodeGenPrepare::optimizePhiType(
AnyAnchored |= !isa<LoadInst>(OpBC->getOperand(0)) &&
!isa<ExtractElementInst>(OpBC->getOperand(0));
}
} else if (!isa<UndefValue>(V)) {
} else if (auto *OpC = dyn_cast<ConstantData>(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<BitCastInst>(D)) {
ValMap[D] = D->getOperand(0);

View File

@ -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