Skip to content

Commit

Permalink
[clang] Return std::string_view from TargetInfo::getClobbers()
Browse files Browse the repository at this point in the history
Change the return type of `getClobbers` function from `const char*`
to `std::string_view`. Update the function usages in CodeGen module.

The reasoning of these changes is to remove unsafe `const char*`
strings and prevent unnecessary allocations for constructing the
`std::string` in usages of `getClobbers()` function.

Differential Revision: https://reviews.llvm.org/D148799
  • Loading branch information
Stoorx committed Apr 24, 2023
1 parent 368112e commit 42d758b
Show file tree
Hide file tree
Showing 33 changed files with 34 additions and 34 deletions.
2 changes: 1 addition & 1 deletion clang/include/clang/Basic/TargetInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -1178,7 +1178,7 @@ class TargetInfo : public TransferrableTargetInfo,
}

/// Returns a string of target-specific clobbers, in LLVM format.
virtual const char *getClobbers() const = 0;
virtual std::string_view getClobbers() const = 0;

/// Returns true if NaN encoding is IEEE 754-2008.
/// Only MIPS allows a different encoding.
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Basic/Targets/AArch64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1295,7 +1295,7 @@ bool AArch64TargetInfo::validateConstraintModifier(
}
}

const char *AArch64TargetInfo::getClobbers() const { return ""; }
std::string_view AArch64TargetInfo::getClobbers() const { return ""; }

int AArch64TargetInfo::getEHDataRegisterNumber(unsigned RegNo) const {
if (RegNo == 0)
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Basic/Targets/AArch64.h
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ class LLVM_LIBRARY_VISIBILITY AArch64TargetInfo : public TargetInfo {
bool
validateConstraintModifier(StringRef Constraint, char Modifier, unsigned Size,
std::string &SuggestedModifier) const override;
const char *getClobbers() const override;
std::string_view getClobbers() const override;

StringRef getConstraintRegister(StringRef Constraint,
StringRef Expression) const override {
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Basic/Targets/AMDGPU.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ class LLVM_LIBRARY_VISIBILITY AMDGPUTargetInfo final : public TargetInfo {
bool hasBFloat16Type() const override { return isAMDGCN(getTriple()); }
const char *getBFloat16Mangling() const override { return "u6__bf16"; };

const char *getClobbers() const override { return ""; }
std::string_view getClobbers() const override { return ""; }

ArrayRef<const char *> getGCCRegNames() const override;

Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Basic/Targets/ARC.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class LLVM_LIBRARY_VISIBILITY ARCTargetInfo : public TargetInfo {
return TargetInfo::VoidPtrBuiltinVaList;
}

const char *getClobbers() const override { return ""; }
std::string_view getClobbers() const override { return ""; }

ArrayRef<const char *> getGCCRegNames() const override {
static const char *const GCCRegNames[] = {
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Basic/Targets/ARM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1244,7 +1244,7 @@ bool ARMTargetInfo::validateConstraintModifier(

return true;
}
const char *ARMTargetInfo::getClobbers() const {
std::string_view ARMTargetInfo::getClobbers() const {
// FIXME: Is this really right?
return "";
}
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Basic/Targets/ARM.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ class LLVM_LIBRARY_VISIBILITY ARMTargetInfo : public TargetInfo {
bool
validateConstraintModifier(StringRef Constraint, char Modifier, unsigned Size,
std::string &SuggestedModifier) const override;
const char *getClobbers() const override;
std::string_view getClobbers() const override;

StringRef getConstraintRegister(StringRef Constraint,
StringRef Expression) const override {
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Basic/Targets/AVR.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class LLVM_LIBRARY_VISIBILITY AVRTargetInfo : public TargetInfo {
return TargetInfo::VoidPtrBuiltinVaList;
}

const char *getClobbers() const override { return ""; }
std::string_view getClobbers() const override { return ""; }

ArrayRef<const char *> getGCCRegNames() const override {
static const char *const GCCRegNames[] = {
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Basic/Targets/BPF.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class LLVM_LIBRARY_VISIBILITY BPFTargetInfo : public TargetInfo {

ArrayRef<Builtin::Info> getTargetBuiltins() const override;

const char *getClobbers() const override { return ""; }
std::string_view getClobbers() const override { return ""; }

BuiltinVaListKind getBuiltinVaListKind() const override {
return TargetInfo::VoidPtrBuiltinVaList;
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Basic/Targets/CSKY.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class LLVM_LIBRARY_VISIBILITY CSKYTargetInfo : public TargetInfo {
bool validateAsmConstraint(const char *&Name,
TargetInfo::ConstraintInfo &info) const override;

const char *getClobbers() const override { return ""; }
std::string_view getClobbers() const override { return ""; }

void getTargetDefines(const LangOptions &Opts,
MacroBuilder &Builder) const override;
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Basic/Targets/DirectX.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class LLVM_LIBRARY_VISIBILITY DirectXTargetInfo : public TargetInfo {
return std::nullopt;
}

const char *getClobbers() const override { return ""; }
std::string_view getClobbers() const override { return ""; }

ArrayRef<const char *> getGCCRegNames() const override {
return std::nullopt;
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Basic/Targets/Hexagon.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class LLVM_LIBRARY_VISIBILITY HexagonTargetInfo : public TargetInfo {

ArrayRef<TargetInfo::GCCRegAlias> getGCCRegAliases() const override;

const char *getClobbers() const override { return ""; }
std::string_view getClobbers() const override { return ""; }

static const char *getHexagonCPUSuffix(StringRef Name);

Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Basic/Targets/Lanai.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class LLVM_LIBRARY_VISIBILITY LanaiTargetInfo : public TargetInfo {
return false;
}

const char *getClobbers() const override { return ""; }
std::string_view getClobbers() const override { return ""; }

bool hasBitIntType() const override { return true; }
};
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Basic/Targets/Le64.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class LLVM_LIBRARY_VISIBILITY Le64TargetInfo : public TargetInfo {
return TargetInfo::PNaClABIBuiltinVaList;
}

const char *getClobbers() const override { return ""; }
std::string_view getClobbers() const override { return ""; }

ArrayRef<const char *> getGCCRegNames() const override {
return std::nullopt;
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Basic/Targets/LoongArch.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class LLVM_LIBRARY_VISIBILITY LoongArchTargetInfo : public TargetInfo {
return TargetInfo::VoidPtrBuiltinVaList;
}

const char *getClobbers() const override { return ""; }
std::string_view getClobbers() const override { return ""; }

ArrayRef<const char *> getGCCRegNames() const override;

Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Basic/Targets/M68k.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ std::string M68kTargetInfo::convertConstraint(const char *&Constraint) const {
return std::string(1, *Constraint);
}

const char *M68kTargetInfo::getClobbers() const {
std::string_view M68kTargetInfo::getClobbers() const {
// FIXME: Is this really right?
return "";
}
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Basic/Targets/M68k.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class LLVM_LIBRARY_VISIBILITY M68kTargetInfo : public TargetInfo {
bool validateAsmConstraint(const char *&Name,
TargetInfo::ConstraintInfo &info) const override;
std::optional<std::string> handleAsmEscapedChar(char EscChar) const override;
const char *getClobbers() const override;
std::string_view getClobbers() const override;
BuiltinVaListKind getBuiltinVaListKind() const override;
bool setCPU(const std::string &Name) override;
};
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Basic/Targets/MSP430.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class LLVM_LIBRARY_VISIBILITY MSP430TargetInfo : public TargetInfo {
return false;
}

const char *getClobbers() const override {
std::string_view getClobbers() const override {
// FIXME: Is this really right?
return "";
}
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Basic/Targets/Mips.h
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ class LLVM_LIBRARY_VISIBILITY MipsTargetInfo : public TargetInfo {
return TargetInfo::convertConstraint(Constraint);
}

const char *getClobbers() const override {
std::string_view getClobbers() const override {
// In GCC, $1 is not widely used in generated code (it's used only in a few
// specific situations), so there is no real need for users to add it to
// the clobbers list if they want to use it in their inline assembly code.
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Basic/Targets/NVPTX.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ class LLVM_LIBRARY_VISIBILITY NVPTXTargetInfo : public TargetInfo {
}
}

const char *getClobbers() const override {
std::string_view getClobbers() const override {
// FIXME: Is this really right?
return "";
}
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Basic/Targets/PNaCl.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class LLVM_LIBRARY_VISIBILITY PNaClTargetInfo : public TargetInfo {
return false;
}

const char *getClobbers() const override { return ""; }
std::string_view getClobbers() const override { return ""; }

bool hasBitIntType() const override { return true; }
};
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Basic/Targets/PPC.h
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ class LLVM_LIBRARY_VISIBILITY PPCTargetInfo : public TargetInfo {
return R;
}

const char *getClobbers() const override { return ""; }
std::string_view getClobbers() const override { return ""; }
int getEHDataRegisterNumber(unsigned RegNo) const override {
if (RegNo == 0)
return 3;
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Basic/Targets/RISCV.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class RISCVTargetInfo : public TargetInfo {
return TargetInfo::VoidPtrBuiltinVaList;
}

const char *getClobbers() const override { return ""; }
std::string_view getClobbers() const override { return ""; }

StringRef getConstraintRegister(StringRef Constraint,
StringRef Expression) const override {
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Basic/Targets/SPIR.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ class LLVM_LIBRARY_VISIBILITY BaseSPIRTargetInfo : public TargetInfo {
return std::nullopt;
}

const char *getClobbers() const override { return ""; }
std::string_view getClobbers() const override { return ""; }

ArrayRef<const char *> getGCCRegNames() const override {
return std::nullopt;
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Basic/Targets/Sparc.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class LLVM_LIBRARY_VISIBILITY SparcTargetInfo : public TargetInfo {
}
return false;
}
const char *getClobbers() const override {
std::string_view getClobbers() const override {
// FIXME: Implement!
return "";
}
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Basic/Targets/SystemZ.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ class LLVM_LIBRARY_VISIBILITY SystemZTargetInfo : public TargetInfo {
return TargetInfo::convertConstraint(Constraint);
}

const char *getClobbers() const override {
std::string_view getClobbers() const override {
// FIXME: Is this really right?
return "";
}
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Basic/Targets/TCE.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class LLVM_LIBRARY_VISIBILITY TCETargetInfo : public TargetInfo {
return std::nullopt;
}

const char *getClobbers() const override { return ""; }
std::string_view getClobbers() const override { return ""; }

BuiltinVaListKind getBuiltinVaListKind() const override {
return TargetInfo::VoidPtrBuiltinVaList;
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Basic/Targets/VE.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class LLVM_LIBRARY_VISIBILITY VETargetInfo : public TargetInfo {
}
}

const char *getClobbers() const override { return ""; }
std::string_view getClobbers() const override { return ""; }

ArrayRef<const char *> getGCCRegNames() const override {
static const char *const GCCRegNames[] = {
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Basic/Targets/WebAssembly.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ class LLVM_LIBRARY_VISIBILITY WebAssemblyTargetInfo : public TargetInfo {
return false;
}

const char *getClobbers() const final { return ""; }
std::string_view getClobbers() const final { return ""; }

bool isCLZForZeroUndef() const final { return false; }

Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Basic/Targets/X86.h
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ class LLVM_LIBRARY_VISIBILITY X86TargetInfo : public TargetInfo {
StringRef Constraint, unsigned Size) const;

std::string convertConstraint(const char *&Constraint) const override;
const char *getClobbers() const override {
std::string_view getClobbers() const override {
return "~{dirflag},~{fpsr},~{flags}";
}

Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Basic/Targets/XCore.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class LLVM_LIBRARY_VISIBILITY XCoreTargetInfo : public TargetInfo {
return TargetInfo::VoidPtrBuiltinVaList;
}

const char *getClobbers() const override { return ""; }
std::string_view getClobbers() const override { return ""; }

ArrayRef<const char *> getGCCRegNames() const override {
static const char *const GCCRegNames[] = {
Expand Down
4 changes: 2 additions & 2 deletions clang/lib/CodeGen/CGBuiltin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -939,7 +939,7 @@ static llvm::Value *EmitX86BitTestIntrinsic(CodeGenFunction &CGF,

// Build the constraints. FIXME: We should support immediates when possible.
std::string Constraints = "={@ccc},r,r,~{cc},~{memory}";
std::string MachineClobbers = CGF.getTarget().getClobbers();
std::string_view MachineClobbers = CGF.getTarget().getClobbers();
if (!MachineClobbers.empty()) {
Constraints += ',';
Constraints += MachineClobbers;
Expand Down Expand Up @@ -1082,7 +1082,7 @@ static llvm::Value *emitPPCLoadReserveIntrinsic(CodeGenFunction &CGF,
AsmOS << "$0, ${1:y}";

std::string Constraints = "=r,*Z,~{memory}";
std::string MachineClobbers = CGF.getTarget().getClobbers();
std::string_view MachineClobbers = CGF.getTarget().getClobbers();
if (!MachineClobbers.empty()) {
Constraints += ',';
Constraints += MachineClobbers;
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/CodeGen/CGStmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2779,7 +2779,7 @@ void CodeGenFunction::EmitAsmStmt(const AsmStmt &S) {
"unwind clobber can't be used with asm goto");

// Add machine specific clobbers
std::string MachineClobbers = getTarget().getClobbers();
std::string_view MachineClobbers = getTarget().getClobbers();
if (!MachineClobbers.empty()) {
if (!Constraints.empty())
Constraints += ',';
Expand Down

0 comments on commit 42d758b

Please sign in to comment.