Skip to content

Commit 24ff5d6

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 5604e7a commit 24ff5d6

File tree

10 files changed

+96
-46
lines changed

10 files changed

+96
-46
lines changed

clang/lib/CodeGen/TargetBuiltins/AMDGPU.cpp

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

206206
// Some of the atomic builtins take the scope as a string name.
207207
StringRef scp;
208-
if (llvm::getConstantStringInfo(Scope, scp)) {
208+
if (llvm::getConstantStringInfo(Scope, scp, /*CharWidth=*/8)) {
209209
SSID = getLLVMContext().getOrInsertSyncScopeID(scp);
210210
return;
211211
}
@@ -260,7 +260,7 @@ void CodeGenFunction::AddAMDGPUFenceAddressSpaceMMRA(llvm::Instruction *Inst,
260260
for (unsigned K = 2; K < E->getNumArgs(); ++K) {
261261
llvm::Value *V = EmitScalarExpr(E->getArg(K));
262262
StringRef AS;
263-
if (llvm::getConstantStringInfo(V, AS)) {
263+
if (llvm::getConstantStringInfo(V, AS, /*CharWidth=*/8)) {
264264
MMRAs.push_back({Tag, AS});
265265
// TODO: Delete the resulting unused constant?
266266
continue;

llvm/include/llvm/Analysis/ValueTracking.h

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

418418
/// 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
@@ -6737,9 +6737,12 @@ bool llvm::getConstantDataArrayInfo(const Value *V,
67376737
/// return true. When TrimAtNul is set, Str will contain only the bytes up
67386738
/// to but not including the first nul. Return false on failure.
67396739
bool llvm::getConstantStringInfo(const Value *V, StringRef &Str,
6740-
bool TrimAtNul) {
6740+
unsigned CharWidth, bool TrimAtNul) {
6741+
if (CharWidth != CHAR_BIT)
6742+
return false;
6743+
67416744
ConstantDataArraySlice Slice;
6742-
if (!getConstantDataArrayInfo(V, Slice, 8))
6745+
if (!getConstantDataArrayInfo(V, Slice, CharWidth))
67436746
return false;
67446747

67456748
if (Slice.Array == nullptr) {

llvm/lib/Target/AMDGPU/AMDGPUPrintfRuntimeBinding.cpp

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

122122
static StringRef getAsConstantStr(Value *V) {
123123
StringRef S;
124-
if (!getConstantStringInfo(V, S))
124+
if (!getConstantStringInfo(V, S, /*CharWidth=*/8))
125125
S = NonLiteralStr;
126126

127127
return S;
@@ -155,7 +155,7 @@ bool AMDGPUPrintfRuntimeBindingImpl::lowerPrintfForGpu(Module &M) {
155155
Value *Op = CI->getArgOperand(0);
156156

157157
StringRef FormatStr;
158-
if (!getConstantStringInfo(Op, FormatStr)) {
158+
if (!getConstantStringInfo(Op, FormatStr, /*CharWidth=*/8)) {
159159
Value *Stripped = Op->stripPointerCasts();
160160
if (!isa<UndefValue>(Stripped) && !isa<ConstantPointerNull>(Stripped))
161161
diagnoseInvalidFormatString(CI);

llvm/lib/Target/SPIRV/SPIRVAsmPrinter.cpp

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

576576
StringRef AnnotationString;
577-
getConstantStringInfo(GV, AnnotationString);
577+
getConstantStringInfo(GV, AnnotationString, /*CharWidth=*/8);
578578
MCInst Inst;
579579
Inst.setOpcode(SPIRV::OpDecorate);
580580
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
@@ -159,7 +159,7 @@ static std::string getAnnotation(Value *AnnoVal, Value *OptAnnoVal) {
159159
std::string Anno;
160160
if (auto *C = dyn_cast_or_null<Constant>(AnnoVal)) {
161161
StringRef Str;
162-
if (getConstantStringInfo(C, Str))
162+
if (getConstantStringInfo(C, Str, /*CharWidth=*/8))
163163
Anno = Str;
164164
}
165165
// 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
@@ -577,7 +577,7 @@ void WebAssemblyAsmPrinter::EmitFunctionAttributes(Module &M) {
577577
// The second field is a pointer to a global annotation string.
578578
auto *GV = cast<GlobalVariable>(CS->getOperand(1)->stripPointerCasts());
579579
StringRef AnnotationString;
580-
getConstantStringInfo(GV, AnnotationString);
580+
getConstantStringInfo(GV, AnnotationString, /*CharWidth=*/8);
581581
auto *Sym = cast<MCSymbolWasm>(getSymbol(F));
582582
CustomSections[AnnotationString].push_back(Sym);
583583
}

llvm/lib/Transforms/AggressiveInstCombine/AggressiveInstCombine.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -965,6 +965,8 @@ class StrNCmpInliner {
965965
/// handled by the instcombine pass.
966966
///
967967
bool StrNCmpInliner::optimizeStrNCmp() {
968+
unsigned CharWidth = DL.getByteWidth();
969+
968970
if (StrNCmpInlineThreshold < 2)
969971
return false;
970972

@@ -978,8 +980,10 @@ bool StrNCmpInliner::optimizeStrNCmp() {
978980
return false;
979981

980982
StringRef Str1, Str2;
981-
bool HasStr1 = getConstantStringInfo(Str1P, Str1, /*TrimAtNul=*/false);
982-
bool HasStr2 = getConstantStringInfo(Str2P, Str2, /*TrimAtNul=*/false);
983+
bool HasStr1 =
984+
getConstantStringInfo(Str1P, Str1, CharWidth, /*TrimAtNul=*/false);
985+
bool HasStr2 =
986+
getConstantStringInfo(Str2P, Str2, CharWidth, /*TrimAtNul=*/false);
983987
if (HasStr1 == HasStr2)
984988
return false;
985989

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

11151121
StringRef Str;
11161122
Value *Base = Call->getArgOperand(0);
1117-
if (!getConstantStringInfo(Base, Str, /*TrimAtNul=*/false))
1123+
if (!getConstantStringInfo(Base, Str, CharWidth, /*TrimAtNul=*/false))
11181124
return false;
11191125

11201126
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)