-
Notifications
You must be signed in to change notification settings - Fork 13.6k
[RISCV] Transform fcmp to is.fpclass #120242
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,11 +20,13 @@ | |
#include "llvm/CodeGen/TargetPassConfig.h" | ||
#include "llvm/IR/Dominators.h" | ||
#include "llvm/IR/IRBuilder.h" | ||
#include "llvm/IR/InstrTypes.h" | ||
#include "llvm/IR/InstVisitor.h" | ||
#include "llvm/IR/Intrinsics.h" | ||
#include "llvm/IR/PatternMatch.h" | ||
#include "llvm/InitializePasses.h" | ||
#include "llvm/Pass.h" | ||
#include "llvm/Transforms/Utils/Local.h" | ||
|
||
using namespace llvm; | ||
|
||
|
@@ -58,6 +60,7 @@ class RISCVCodeGenPrepare : public FunctionPass, | |
bool visitAnd(BinaryOperator &BO); | ||
bool visitIntrinsicInst(IntrinsicInst &I); | ||
bool expandVPStrideLoad(IntrinsicInst &I); | ||
bool visitFCmpInst(FCmpInst &I); | ||
}; | ||
|
||
} // end anonymous namespace | ||
|
@@ -196,6 +199,42 @@ bool RISCVCodeGenPrepare::expandVPStrideLoad(IntrinsicInst &II) { | |
return true; | ||
} | ||
|
||
// The 'fcmp uno/ord/oeq/une/ueq/one/ogt/oge/olt/ole x, 0.0' instructions are | ||
// equivalent to an FP class test. If the fcmp instruction would be custom | ||
// lowered or lowered to a libcall, use the is.fpclass intrinsic instead, which | ||
// is lowered by the back-end without a libcall. | ||
// | ||
// This basically reverts the transformations of | ||
// InstCombinerImpl::foldIntrinsicIsFPClass. | ||
bool RISCVCodeGenPrepare::visitFCmpInst(FCmpInst &Fcmp) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. CodeGenPrepare already has this transform |
||
const auto *TLI = ST->getTargetLowering(); | ||
const EVT VT = TLI->getValueType(*DL, Fcmp.getOperand(0)->getType()); | ||
const int ISDOpcode = TLI->InstructionOpcodeToISD(Fcmp.getOpcode()); | ||
|
||
auto LegalizeTypeAction = TLI->getTypeAction(Fcmp.getContext(), VT); | ||
auto OperationAction = TLI->getOperationAction(ISDOpcode, VT); | ||
if ((LegalizeTypeAction != TargetLoweringBase::TypeSoftenFloat && | ||
LegalizeTypeAction != TargetLoweringBase::TypeSoftPromoteHalf) || | ||
OperationAction == TargetLowering::Custom) | ||
return false; | ||
Comment on lines
+214
to
+219
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This level of logic really belongs directly in the legalizer There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, I understand. Moving it to the legalizer. @topperc is it ok to implement it there? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually the logic in CodeGenPrepare uses There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't really like having it in codegenprepare in the first place. It really belongs in some combination of DAGCombiner or legalizer, depending on the purpose. The only nice thing is codegenprepare has access to better utilities, like an existing fcmpToClassTest helper and computeKnownFPClass. In principle those should be reimplemented in codegen There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok. Regarding the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fcmp is a better canonical form. More code will always understand fcmp than is.fpclass.
This is certainly not universally true, and I would say is not the common case. If the target wants something else, that's for the backend to undo for its preferred form. |
||
|
||
auto [ClassVal, ClassTest] = | ||
fcmpToClassTest(Fcmp.getPredicate(), *Fcmp.getParent()->getParent(), | ||
Fcmp.getOperand(0), Fcmp.getOperand(1)); | ||
|
||
// FIXME: For some conditions (e.g ole, olt, oge, ogt) the output is quite | ||
// verbose compared to the libcall. Should we do the tranformation | ||
// only if we are optimizing for speed? | ||
if (!ClassVal) | ||
return false; | ||
|
||
IRBuilder<> Builder(&Fcmp); | ||
Value *IsFPClass = Builder.createIsFPClass(ClassVal, ClassTest); | ||
Fcmp.replaceAllUsesWith(IsFPClass); | ||
RecursivelyDeleteTriviallyDeadInstructions(&Fcmp); | ||
return true; | ||
} | ||
|
||
bool RISCVCodeGenPrepare::runOnFunction(Function &F) { | ||
if (skipFunction(F)) | ||
return false; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't true depending on denormal handling and fp exceptions