llvm.org GIT mirror llvm / fb3a628
[InstCombine] Split the FP constant code out of lookThroughFPExtensions and use nullptr as a sentinel Currently this code's control flow very much assumes that there are no meaningful checks after determining that it's a ConstantFP. So whenever it wants to stop it just does "return V". But V is also the variable name it uses when it wants to return a new value. So 'return V' appears multiple times with different meanings. This patch just moves all the code into a helper function and returns nullptr when it wants to stop. I've split this from D43774 while I try to figure out how to best handle the vector case there. But this change by itself at least seemed like a readability improvement. Differential Revision: https://reviews.llvm.org/D43833 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@326361 91177308-0d34-0410-b5e6-96231b3b80d8 Craig Topper 2 years ago
1 changed file(s) with 20 addition(s) and 15 deletion(s). Raw diff Collapse all Expand all
14191419 return nullptr;
14201420 }
14211421
1422 static Constant *shrinkFPConstant(ConstantFP *CFP) {
1423 if (CFP->getType() == Type::getPPC_FP128Ty(CFP->getContext()))
1424 return nullptr; // No constant folding of this.
1425 // See if the value can be truncated to half and then reextended.
1426 if (Constant *NewCFP = fitsInFPType(CFP, APFloat::IEEEhalf()))
1427 return NewCFP;
1428 // See if the value can be truncated to float and then reextended.
1429 if (Constant *NewCFP = fitsInFPType(CFP, APFloat::IEEEsingle()))
1430 return NewCFP;
1431 if (CFP->getType()->isDoubleTy())
1432 return nullptr; // Won't shrink.
1433 if (Constant *NewCFP = fitsInFPType(CFP, APFloat::IEEEdouble()))
1434 return NewCFP;
1435 // Don't try to shrink to various long double types.
1436 return nullptr;
1437 }
1438
14221439 /// Look through floating-point extensions until we get the source value.
14231440 static Value *lookThroughFPExtensions(Value *V) {
14241441 while (auto *FPExt = dyn_cast(V))
14271444 // If this value is a constant, return the constant in the smallest FP type
14281445 // that can accurately represent it. This allows us to turn
14291446 // (float)((double)X+2.0) into x+2.0f.
1430 if (auto *CFP = dyn_cast(V)) {
1431 if (CFP->getType() == Type::getPPC_FP128Ty(V->getContext()))
1432 return V; // No constant folding of this.
1433 // See if the value can be truncated to half and then reextended.
1434 if (Value *V = fitsInFPType(CFP, APFloat::IEEEhalf()))
1435 return V;
1436 // See if the value can be truncated to float and then reextended.
1437 if (Value *V = fitsInFPType(CFP, APFloat::IEEEsingle()))
1438 return V;
1439 if (CFP->getType()->isDoubleTy())
1440 return V; // Won't shrink.
1441 if (Value *V = fitsInFPType(CFP, APFloat::IEEEdouble()))
1442 return V;
1443 // Don't try to shrink to various long double types.
1444 }
1447 if (auto *CFP = dyn_cast(V))
1448 if (Constant *NewCFP = shrinkFPConstant(CFP))
1449 return NewCFP;
14451450
14461451 return V;
14471452 }