-
Notifications
You must be signed in to change notification settings - Fork 14.5k
[RISCV] Implement EmitTargetCodeForStrcmp
for unaligned case.
#86645
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
//===-- RISCVSelectionDAGTargetInfo.cpp - RISCV SelectionDAG Info | ||
//-----------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This file implements the RISCVSelectionDAGTargetInfo class. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "RISCVSelectionDAGTargetInfo.h" | ||
#include "RISCVSubtarget.h" | ||
#include "llvm/CodeGen/SelectionDAG.h" | ||
#include "llvm/IR/GlobalValue.h" | ||
#include "llvm/IR/GlobalVariable.h" | ||
#include "llvm/IR/Type.h" | ||
|
||
using namespace llvm; | ||
|
||
#define DEBUG_TYPE "riscv-selectiondag-target-info" | ||
|
||
static cl::opt<unsigned> MaxStrcmpSpecializeLength( | ||
"riscv-max-strcmp-specialize-length", cl::Hidden, | ||
cl::desc("Do not specialize strcmp if the length of constant string is " | ||
"greater or equal to this parameter"), | ||
cl::init(0)); | ||
|
||
static bool canSpecializeStrcmp(const GlobalAddressSDNode *GA) { | ||
const GlobalVariable *GV = dyn_cast<GlobalVariable>(GA->getGlobal()); | ||
if (!GV || !GV->isConstant() || !GV->hasInitializer()) | ||
return false; | ||
// NOTE: this doesn't work for empty strings | ||
const ConstantDataArray *CDA = | ||
dyn_cast<ConstantDataArray>(GV->getInitializer()); | ||
if (!CDA || !CDA->isCString()) | ||
return false; | ||
|
||
StringRef CString = CDA->getAsCString(); | ||
if (CString.str().length() >= MaxStrcmpSpecializeLength) | ||
return false; | ||
|
||
return true; | ||
} | ||
|
||
std::pair<SDValue, SDValue> | ||
RISCVSelectionDAGTargetInfo::EmitTargetCodeForStrcmp( | ||
SelectionDAG &DAG, const SDLoc &DL, SDValue Chain, SDValue Src1, | ||
SDValue Src2, MachinePointerInfo Op1PtrInfo, | ||
MachinePointerInfo Op2PtrInfo) const { | ||
// This is the default setting, so exit early if the optimization is turned | ||
// off. | ||
if (MaxStrcmpSpecializeLength == 0) | ||
return std::make_pair(SDValue(), Chain); | ||
|
||
const RISCVSubtarget &Subtarget = | ||
DAG.getMachineFunction().getSubtarget<RISCVSubtarget>(); | ||
const TargetLowering &TLI = DAG.getTargetLoweringInfo(); | ||
MVT XLenVT = Subtarget.getXLenVT(); | ||
const DataLayout &DLayout = DAG.getDataLayout(); | ||
|
||
Align NeededAlignment = Align(XLenVT.getSizeInBits() / 8); | ||
Align Src1Align; | ||
Align Src2Align; | ||
if (const Value *Src1V = dyn_cast_if_present<const Value *>(Op1PtrInfo.V)) { | ||
Src1Align = Src1V->getPointerAlignment(DLayout); | ||
} | ||
if (const Value *Src2V = dyn_cast_if_present<const Value *>(Op2PtrInfo.V)) { | ||
Src2Align = Src2V->getPointerAlignment(DLayout); | ||
} | ||
if (!(Src1Align < NeededAlignment || Src2Align < NeededAlignment)) | ||
return std::make_pair(SDValue(), Chain); | ||
|
||
const GlobalAddressSDNode *CStringGA = nullptr; | ||
SDValue Other; | ||
MachinePointerInfo MPI; | ||
bool ConstantStringIsSecond = false; | ||
|
||
const GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Src1); | ||
if (GA && canSpecializeStrcmp(GA)) { | ||
CStringGA = GA; | ||
Other = Src2; | ||
MPI = Op2PtrInfo; | ||
} | ||
if (!CStringGA) { | ||
GA = dyn_cast<GlobalAddressSDNode>(Src2); | ||
if (GA && canSpecializeStrcmp(GA)) { | ||
ConstantStringIsSecond = true; | ||
CStringGA = GA; | ||
Other = Src1; | ||
MPI = Op1PtrInfo; | ||
} | ||
} | ||
|
||
if (!CStringGA) | ||
return std::make_pair(SDValue(), Chain); | ||
|
||
// It could be that the non-constant string is actually aligned, but | ||
// we can't prove it, so getPointerAlignment will return Align(1). | ||
// In this case, if the constant string is sufficiently aligned, It is better | ||
// to call to libc's strcmp? | ||
Align ConstantStrAlignment = ConstantStringIsSecond ? Src2Align : Src1Align; | ||
if (ConstantStrAlignment >= NeededAlignment) | ||
return std::make_pair(SDValue(), Chain); | ||
|
||
SDValue TGA = DAG.getTargetGlobalAddress(CStringGA->getGlobal(), DL, | ||
TLI.getPointerTy(DLayout), 0, | ||
CStringGA->getTargetFlags()); | ||
|
||
SDValue Str1 = TGA; | ||
SDValue Str2 = Other; | ||
if (ConstantStringIsSecond) | ||
std::swap(Str1, Str2); | ||
|
||
MachineFunction &MF = DAG.getMachineFunction(); | ||
MachineMemOperand *MMO = MF.getMachineMemOperand( | ||
MPI, MachineMemOperand::MOLoad, LLT(MVT::i8), Align(1)); | ||
// TODO: what should be the MemVT? | ||
SDValue STRCMPNode = DAG.getMemIntrinsicNode( | ||
RISCVISD::STRCMP, DL, DAG.getVTList(XLenVT, MVT::Other), | ||
{Chain, Str1, Str2}, MVT::i8, MMO); | ||
|
||
SDValue ChainOut = STRCMPNode.getValue(1); | ||
return std::make_pair(STRCMPNode, ChainOut); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
//===-- RISCVSelectionDAGTargetInfo.h - RISCV SelectionDAG Info ---*- C++ | ||
//-*-===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This file defines the RISCV subclass for SelectionDAGTargetInfo. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef LLVM_LIB_TARGET_RISCV_RISCVSELECTIONDAGINFO_H | ||
#define LLVM_LIB_TARGET_RISCV_RISCVSELECTIONDAGINFO_H | ||
|
||
#include "llvm/CodeGen/SelectionDAGTargetInfo.h" | ||
|
||
namespace llvm { | ||
|
||
class RISCVSelectionDAGTargetInfo : public SelectionDAGTargetInfo { | ||
public: | ||
explicit RISCVSelectionDAGTargetInfo() = default; | ||
std::pair<SDValue, SDValue> | ||
EmitTargetCodeForStrcmp(SelectionDAG &DAG, const SDLoc &DL, SDValue Chain, | ||
SDValue Src1, SDValue Src2, | ||
MachinePointerInfo Op1PtrInfo, | ||
MachinePointerInfo Op2PtrInfo) const override; | ||
}; | ||
|
||
} // end namespace llvm | ||
|
||
#endif |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
What is the proper way to indicate that this will read a known number of bytes from its argument?