Skip to content

Commit cd9a02e

Browse files
committed
[CodeGen] Remove useless zero-index constant GEPs (NFCI)
Remove zero-index constant expression GEPs, which are not needed with opaque pointers and will get folded away.
1 parent 8bce40b commit cd9a02e

File tree

4 files changed

+8
-28
lines changed

4 files changed

+8
-28
lines changed

clang/lib/CodeGen/CGBuiltin.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5978,11 +5978,7 @@ RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
59785978
auto Name = CGM.getCUDARuntime().getDeviceSideName(
59795979
cast<DeclRefExpr>(E->getArg(0)->IgnoreImpCasts())->getDecl());
59805980
auto Str = CGM.GetAddrOfConstantCString(Name, "");
5981-
llvm::Constant *Zeros[] = {llvm::ConstantInt::get(SizeTy, 0),
5982-
llvm::ConstantInt::get(SizeTy, 0)};
5983-
auto *Ptr = llvm::ConstantExpr::getGetElementPtr(Str.getElementType(),
5984-
Str.getPointer(), Zeros);
5985-
return RValue::get(Ptr);
5981+
return RValue::get(Str.getPointer());
59865982
}
59875983
}
59885984

clang/lib/CodeGen/CGCUDANV.cpp

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,6 @@ class CGNVCUDARuntime : public CGCUDARuntime {
7171
bool RelocatableDeviceCode;
7272
/// Mangle context for device.
7373
std::unique_ptr<MangleContext> DeviceMC;
74-
/// Some zeros used for GEPs.
75-
llvm::Constant *Zeros[2];
7674

7775
llvm::FunctionCallee getSetupArgumentFn() const;
7876
llvm::FunctionCallee getLaunchFn() const;
@@ -91,9 +89,7 @@ class CGNVCUDARuntime : public CGCUDARuntime {
9189
/// where the C code specifies const char*.
9290
llvm::Constant *makeConstantString(const std::string &Str,
9391
const std::string &Name = "") {
94-
auto ConstStr = CGM.GetAddrOfConstantCString(Str, Name.c_str());
95-
return llvm::ConstantExpr::getGetElementPtr(ConstStr.getElementType(),
96-
ConstStr.getPointer(), Zeros);
92+
return CGM.GetAddrOfConstantCString(Str, Name.c_str()).getPointer();
9793
}
9894

9995
/// Helper function which generates an initialized constant array from Str,
@@ -117,7 +113,7 @@ class CGNVCUDARuntime : public CGCUDARuntime {
117113
}
118114
if (Alignment)
119115
GV->setAlignment(llvm::Align(Alignment));
120-
return llvm::ConstantExpr::getGetElementPtr(GV->getValueType(), GV, Zeros);
116+
return GV;
121117
}
122118

123119
/// Helper function that generates an empty dummy function returning void.
@@ -230,8 +226,6 @@ CGNVCUDARuntime::CGNVCUDARuntime(CodeGenModule &CGM)
230226
IntTy = CGM.IntTy;
231227
SizeTy = CGM.SizeTy;
232228
VoidTy = CGM.VoidTy;
233-
Zeros[0] = llvm::ConstantInt::get(SizeTy, 0);
234-
Zeros[1] = Zeros[0];
235229
PtrTy = CGM.UnqualPtrTy;
236230
}
237231

clang/lib/CodeGen/CGObjCGNU.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,7 @@ class CGObjCGNU : public CGObjCRuntime {
199199
llvm::Constant *MakeConstantString(StringRef Str, const char *Name = "") {
200200
ConstantAddress Array =
201201
CGM.GetAddrOfConstantCString(std::string(Str), Name);
202-
return llvm::ConstantExpr::getGetElementPtr(Array.getElementType(),
203-
Array.getPointer(), Zeros);
202+
return Array.getPointer();
204203
}
205204

206205
/// Emits a linkonce_odr string, whose name is the prefix followed by the
@@ -221,8 +220,7 @@ class CGObjCGNU : public CGObjCRuntime {
221220
GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
222221
ConstStr = GV;
223222
}
224-
return llvm::ConstantExpr::getGetElementPtr(ConstStr->getValueType(),
225-
ConstStr, Zeros);
223+
return ConstStr;
226224
}
227225

228226
/// Returns a property name and encoding string.
@@ -1477,8 +1475,7 @@ class CGObjCGNUstep2 : public CGObjCGNUstep {
14771475
GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
14781476
TypesGlobal = GV;
14791477
}
1480-
return llvm::ConstantExpr::getGetElementPtr(TypesGlobal->getValueType(),
1481-
TypesGlobal, Zeros);
1478+
return TypesGlobal;
14821479
}
14831480
llvm::Constant *GetConstantSelector(Selector Sel,
14841481
const std::string &TypeEncoding) override {

clang/lib/CodeGen/CodeGenModule.cpp

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6131,9 +6131,6 @@ CodeGenModule::GetAddrOfConstantCFString(const StringLiteral *Literal) {
61316131
return ConstantAddress(
61326132
C, C->getValueType(), CharUnits::fromQuantity(C->getAlignment()));
61336133

6134-
llvm::Constant *Zero = llvm::Constant::getNullValue(Int32Ty);
6135-
llvm::Constant *Zeros[] = { Zero, Zero };
6136-
61376134
const ASTContext &Context = getContext();
61386135
const llvm::Triple &Triple = getTriple();
61396136

@@ -6204,8 +6201,7 @@ CodeGenModule::GetAddrOfConstantCFString(const StringLiteral *Literal) {
62046201

62056202
// Decay array -> ptr
62066203
CFConstantStringClassRef =
6207-
IsSwiftABI ? llvm::ConstantExpr::getPtrToInt(C, Ty)
6208-
: llvm::ConstantExpr::getGetElementPtr(Ty, C, Zeros);
6204+
IsSwiftABI ? llvm::ConstantExpr::getPtrToInt(C, Ty) : C;
62096205
}
62106206

62116207
QualType CFTy = Context.getCFConstantStringType();
@@ -6261,10 +6257,7 @@ CodeGenModule::GetAddrOfConstantCFString(const StringLiteral *Literal) {
62616257
GV->setSection(".rodata");
62626258

62636259
// String.
6264-
llvm::Constant *Str =
6265-
llvm::ConstantExpr::getGetElementPtr(GV->getValueType(), GV, Zeros);
6266-
6267-
Fields.add(Str);
6260+
Fields.add(GV);
62686261

62696262
// String length.
62706263
llvm::IntegerType *LengthTy =

0 commit comments

Comments
 (0)