Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion llvm/lib/IR/Value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -776,7 +776,7 @@ const Value *Value::stripAndAccumulateConstantOffsets(
Operator::getOpcode(V) == Instruction::AddrSpaceCast) {
V = cast<Operator>(V)->getOperand(0);
} else if (auto *GA = dyn_cast<GlobalAlias>(V)) {
if (!GA->isInterposable())
if (!GA->isInterposable() && GA->getAliasee())
V = GA->getAliasee();
} else if (const auto *Call = dyn_cast<CallBase>(V)) {
if (const Value *RV = Call->getReturnedArgOperand())
Expand Down
31 changes: 31 additions & 0 deletions llvm/unittests/IR/InstructionsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "llvm-c/Core.h"
#include "llvm/ADT/CombinationGenerator.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/Analysis/TargetFolder.h"
#include "llvm/Analysis/ValueTracking.h"
#include "llvm/Analysis/VectorUtils.h"
#include "llvm/AsmParser/Parser.h"
Expand Down Expand Up @@ -874,6 +875,36 @@ TEST(InstructionsTest, GEPIndices) {
delete GEPI;
}

TEST(InstructionsTest, PtrToIntGEPFolding) {
LLVMContext Context;
llvm::Module *M = new llvm::Module("PtrToIntGEPFolding", Context);
llvm::IRBuilder<llvm::TargetFolder> Builder(
Context, llvm::TargetFolder(M->getDataLayout()));

auto *Int64Ty = Builder.getInt64Ty();
auto *ArrTy = llvm::ArrayType::get(Int64Ty, 1);
auto *StructTy = llvm::StructType::create(Context, {ArrTy}, "anon_struct");

// Create alias @m2 to @0
llvm::GlobalAlias *AliasM2 = llvm::GlobalAlias::create(
StructTy,
0, // address space
llvm::GlobalValue::InternalLinkage, "m2", (llvm::Constant *)nullptr, M);

// Build getelementptr for ptrtoint
std::vector<llvm::Value *> GEPIdxs = {
llvm::ConstantInt::get(llvm::Type::getInt32Ty(Context), 0),
llvm::ConstantInt::get(llvm::Type::getInt32Ty(Context), 0),
llvm::ConstantInt::get(llvm::Type::getInt32Ty(Context), 1)};
llvm::Value *GEP = Builder.CreateGEP(StructTy, AliasM2, GEPIdxs, "",
llvm::GEPNoWrapFlags::inBounds());
// Make sure stripAndAccumulateConstantOffsets can properly handle
// GEPs with nullptr global aliasees.
llvm::Value *PtrToInt = Builder.CreatePtrToInt(GEP, Int64Ty);
EXPECT_TRUE(PtrToInt);
delete M;
}

TEST(InstructionsTest, ZeroIndexGEP) {
LLVMContext Context;
DataLayout DL;
Expand Down