Skip to content

Commit e5b70fc

Browse files
committed
[ValueTracking] Add CharWidth argument to getConstantStringInfo (NFC)
The method assumes that host chars and target chars have the same width. Add a CharWidth argument so that it can bail out if the requested char width differs from the host char width. Alternatively, the check could be done at call sites, but this is more error-prone. In the future, this method will be replaced with a different one that allows host/target chars to have different widths. The prototype will be the same except that StringRef is replaced with something that is byte width agnostic. Adding CharWidth argument now reduces the future diff.
1 parent 7cca421 commit e5b70fc

File tree

10 files changed

+96
-46
lines changed

10 files changed

+96
-46
lines changed

clang/lib/CodeGen/CGBuiltin.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18451,7 +18451,7 @@ void CodeGenFunction::ProcessOrderScopeAMDGCN(Value *Order, Value *Scope,
1845118451

1845218452
// Some of the atomic builtins take the scope as a string name.
1845318453
StringRef scp;
18454-
if (llvm::getConstantStringInfo(Scope, scp)) {
18454+
if (llvm::getConstantStringInfo(Scope, scp, /*CharWidth=*/8)) {
1845518455
SSID = getLLVMContext().getOrInsertSyncScopeID(scp);
1845618456
return;
1845718457
}
@@ -18713,7 +18713,7 @@ void CodeGenFunction::AddAMDGPUFenceAddressSpaceMMRA(llvm::Instruction *Inst,
1871318713
for (unsigned K = 2; K < E->getNumArgs(); ++K) {
1871418714
llvm::Value *V = EmitScalarExpr(E->getArg(K));
1871518715
StringRef AS;
18716-
if (llvm::getConstantStringInfo(V, AS)) {
18716+
if (llvm::getConstantStringInfo(V, AS, /*CharWidth=*/8)) {
1871718717
MMRAs.push_back({Tag, AS});
1871818718
// TODO: Delete the resulting unused constant?
1871918719
continue;

llvm/include/llvm/Analysis/ValueTracking.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -692,7 +692,7 @@ bool getConstantDataArrayInfo(const Value *V, ConstantDataArraySlice &Slice,
692692
/// character by default. If TrimAtNul is set to false, then this returns any
693693
/// trailing null characters as well as any other characters that come after
694694
/// it.
695-
bool getConstantStringInfo(const Value *V, StringRef &Str,
695+
bool getConstantStringInfo(const Value *V, StringRef &Str, unsigned CharWidth,
696696
bool TrimAtNul = true);
697697

698698
/// If we can compute the length of the string pointed to by the specified

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6336,9 +6336,12 @@ bool llvm::getConstantDataArrayInfo(const Value *V,
63366336
/// return true. When TrimAtNul is set, Str will contain only the bytes up
63376337
/// to but not including the first nul. Return false on failure.
63386338
bool llvm::getConstantStringInfo(const Value *V, StringRef &Str,
6339-
bool TrimAtNul) {
6339+
unsigned CharWidth, bool TrimAtNul) {
6340+
if (CharWidth != CHAR_BIT)
6341+
return false;
6342+
63406343
ConstantDataArraySlice Slice;
6341-
if (!getConstantDataArrayInfo(V, Slice, 8))
6344+
if (!getConstantDataArrayInfo(V, Slice, CharWidth))
63426345
return false;
63436346

63446347
if (Slice.Array == nullptr) {

llvm/lib/Target/AMDGPU/AMDGPUPrintfRuntimeBinding.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ static_assert(NonLiteralStr.size() == 3);
127127

128128
static StringRef getAsConstantStr(Value *V) {
129129
StringRef S;
130-
if (!getConstantStringInfo(V, S))
130+
if (!getConstantStringInfo(V, S, /*CharWidth=*/8))
131131
S = NonLiteralStr;
132132

133133
return S;
@@ -161,7 +161,7 @@ bool AMDGPUPrintfRuntimeBindingImpl::lowerPrintfForGpu(Module &M) {
161161
Value *Op = CI->getArgOperand(0);
162162

163163
StringRef FormatStr;
164-
if (!getConstantStringInfo(Op, FormatStr)) {
164+
if (!getConstantStringInfo(Op, FormatStr, /*CharWidth=*/8)) {
165165
Value *Stripped = Op->stripPointerCasts();
166166
if (!isa<UndefValue>(Stripped) && !isa<ConstantPointerNull>(Stripped))
167167
diagnoseInvalidFormatString(CI);

llvm/lib/Target/SPIRV/SPIRVAsmPrinter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,7 @@ void SPIRVAsmPrinter::outputAnnotations(const Module &M) {
547547
cast<GlobalVariable>(CS->getOperand(1)->stripPointerCasts());
548548

549549
StringRef AnnotationString;
550-
getConstantStringInfo(GV, AnnotationString);
550+
getConstantStringInfo(GV, AnnotationString, /*CharWidth=*/8);
551551
MCInst Inst;
552552
Inst.setOpcode(SPIRV::OpDecorate);
553553
Inst.addOperand(MCOperand::createReg(Reg));

llvm/lib/Target/SPIRV/SPIRVPrepareFunctions.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ static std::string getAnnotation(Value *AnnoVal, Value *OptAnnoVal) {
164164
std::string Anno;
165165
if (auto *C = dyn_cast_or_null<Constant>(AnnoVal)) {
166166
StringRef Str;
167-
if (getConstantStringInfo(C, Str))
167+
if (getConstantStringInfo(C, Str, /*CharWidth=*/8))
168168
Anno = Str;
169169
}
170170
// handle optional annotation parameter in a way that Khronos Translator do

llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -579,7 +579,7 @@ void WebAssemblyAsmPrinter::EmitFunctionAttributes(Module &M) {
579579
// The second field is a pointer to a global annotation string.
580580
auto *GV = cast<GlobalVariable>(CS->getOperand(1)->stripPointerCasts());
581581
StringRef AnnotationString;
582-
getConstantStringInfo(GV, AnnotationString);
582+
getConstantStringInfo(GV, AnnotationString, /*CharWidth=*/8);
583583
auto *Sym = cast<MCSymbolWasm>(getSymbol(F));
584584
CustomSections[AnnotationString].push_back(Sym);
585585
}

llvm/lib/Transforms/AggressiveInstCombine/AggressiveInstCombine.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -971,6 +971,8 @@ class StrNCmpInliner {
971971
/// handled by the instcombine pass.
972972
///
973973
bool StrNCmpInliner::optimizeStrNCmp() {
974+
unsigned CharWidth = DL.getByteWidth();
975+
974976
if (StrNCmpInlineThreshold < 2)
975977
return false;
976978

@@ -984,8 +986,10 @@ bool StrNCmpInliner::optimizeStrNCmp() {
984986
return false;
985987

986988
StringRef Str1, Str2;
987-
bool HasStr1 = getConstantStringInfo(Str1P, Str1, /*TrimAtNul=*/false);
988-
bool HasStr2 = getConstantStringInfo(Str2P, Str2, /*TrimAtNul=*/false);
989+
bool HasStr1 =
990+
getConstantStringInfo(Str1P, Str1, CharWidth, /*TrimAtNul=*/false);
991+
bool HasStr2 =
992+
getConstantStringInfo(Str2P, Str2, CharWidth, /*TrimAtNul=*/false);
989993
if (HasStr1 == HasStr2)
990994
return false;
991995

@@ -1108,12 +1112,14 @@ void StrNCmpInliner::inlineCompare(Value *LHS, StringRef RHS, uint64_t N,
11081112
/// Convert memchr with a small constant string into a switch
11091113
static bool foldMemChr(CallInst *Call, DomTreeUpdater *DTU,
11101114
const DataLayout &DL) {
1115+
unsigned CharWidth = DL.getByteWidth();
1116+
11111117
if (isa<Constant>(Call->getArgOperand(1)))
11121118
return false;
11131119

11141120
StringRef Str;
11151121
Value *Base = Call->getArgOperand(0);
1116-
if (!getConstantStringInfo(Base, Str, /*TrimAtNul=*/false))
1122+
if (!getConstantStringInfo(Base, Str, CharWidth, /*TrimAtNul=*/false))
11171123
return false;
11181124

11191125
uint64_t N = Str.size();

llvm/lib/Transforms/Utils/AMDGPUEmitPrintf.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ static Value *callBufferedPrintfStart(
250250
for (size_t i = 1; i < Args.size(); i++) {
251251
if (SpecIsCString.test(i)) {
252252
StringRef ArgStr;
253-
if (getConstantStringInfo(Args[i], ArgStr)) {
253+
if (getConstantStringInfo(Args[i], ArgStr, /*CharWidth=*/8)) {
254254
auto alignedLen = alignTo(ArgStr.size() + 1, 8);
255255
StringContents.push_back(StringData(
256256
ArgStr,
@@ -432,7 +432,7 @@ Value *llvm::emitAMDGPUPrintfCall(IRBuilder<> &Builder, ArrayRef<Value *> Args,
432432
SparseBitVector<8> SpecIsCString;
433433
StringRef FmtStr;
434434

435-
if (getConstantStringInfo(Fmt, FmtStr))
435+
if (getConstantStringInfo(Fmt, FmtStr, /*CharWidth=*/8))
436436
locateCStrings(SpecIsCString, FmtStr);
437437

438438
if (IsBuffered) {

0 commit comments

Comments
 (0)