[llvm] Clean up after transition into opaque pointers. NFC (PR #102631)

Björn Pettersson via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 12 04:27:36 PDT 2024


https://github.com/bjope updated https://github.com/llvm/llvm-project/pull/102631

>From fb4e5f8d630b1c469304a3b6bf9c58838e0845a6 Mon Sep 17 00:00:00 2001
From: Bjorn Pettersson <bjorn.a.pettersson at ericsson.com>
Date: Tue, 6 Aug 2024 01:18:51 +0200
Subject: [PATCH 1/3] Clean up after transition into opaque pointers. NFC

LegacyPointerTypes is not used any longer and can be removed from
the LLVM context.

Also remove a copy-pasted code comment in TypedPointerType that
doesn't make sense (since there is no special case for address space
zero in the TypedPointerType::get implementation).
---
 llvm/lib/IR/LLVMContextImpl.h    | 1 -
 llvm/lib/IR/TypedPointerType.cpp | 1 -
 2 files changed, 2 deletions(-)

diff --git a/llvm/lib/IR/LLVMContextImpl.h b/llvm/lib/IR/LLVMContextImpl.h
index 8e9ca21d149f65..e76f004b590efe 100644
--- a/llvm/lib/IR/LLVMContextImpl.h
+++ b/llvm/lib/IR/LLVMContextImpl.h
@@ -1608,7 +1608,6 @@ class LLVMContextImpl {
   DenseMap<std::pair<Type *, ElementCount>, VectorType *> VectorTypes;
   PointerType *AS0PointerType = nullptr; // AddrSpace = 0
   DenseMap<unsigned, PointerType *> PointerTypes;
-  DenseMap<std::pair<Type *, unsigned>, PointerType *> LegacyPointerTypes;
   DenseMap<std::pair<Type *, unsigned>, TypedPointerType *> ASTypedPointerTypes;
 
   /// ValueHandles - This map keeps track of all of the value handles that are
diff --git a/llvm/lib/IR/TypedPointerType.cpp b/llvm/lib/IR/TypedPointerType.cpp
index ed91080ba8b8b2..85f7a5a2ae4c30 100644
--- a/llvm/lib/IR/TypedPointerType.cpp
+++ b/llvm/lib/IR/TypedPointerType.cpp
@@ -20,7 +20,6 @@ TypedPointerType *TypedPointerType::get(Type *EltTy, unsigned AddressSpace) {
 
   LLVMContextImpl *CImpl = EltTy->getContext().pImpl;
 
-  // Since AddressSpace #0 is the common case, we special case it.
   TypedPointerType *&Entry =
       CImpl->ASTypedPointerTypes[std::make_pair(EltTy, AddressSpace)];
 

>From 0bc635f178327d138a5ead48895948c12b1c1b8c Mon Sep 17 00:00:00 2001
From: Bjorn Pettersson <bjorn.a.pettersson at ericsson.com>
Date: Fri, 9 Aug 2024 13:46:33 +0200
Subject: [PATCH 2/3] [verifier] Get rid of getResolverFunctionType. NFC

With opaque pointers we can just get the pointer type for the
resolver function by using PointerType::get, making the
GlobalIFunc::getResolverFunctionType function obsolete.
---
 llvm/include/llvm/IR/GlobalIFunc.h | 4 ----
 llvm/lib/IR/Verifier.cpp           | 4 +---
 2 files changed, 1 insertion(+), 7 deletions(-)

diff --git a/llvm/include/llvm/IR/GlobalIFunc.h b/llvm/include/llvm/IR/GlobalIFunc.h
index 4d1982da0baff3..8935284f32d759 100644
--- a/llvm/include/llvm/IR/GlobalIFunc.h
+++ b/llvm/include/llvm/IR/GlobalIFunc.h
@@ -80,10 +80,6 @@ class GlobalIFunc final : public GlobalObject, public ilist_node<GlobalIFunc> {
         static_cast<const GlobalIFunc *>(this)->getResolverFunction());
   }
 
-  static FunctionType *getResolverFunctionType(Type *IFuncValTy) {
-    return FunctionType::get(IFuncValTy->getPointerTo(), false);
-  }
-
   static bool isValidLinkage(LinkageTypes L) {
     return isExternalLinkage(L) || isLocalLinkage(L) || isWeakLinkage(L) ||
            isLinkOnceLinkage(L);
diff --git a/llvm/lib/IR/Verifier.cpp b/llvm/lib/IR/Verifier.cpp
index 402af376651eb6..7d71ce3230b204 100644
--- a/llvm/lib/IR/Verifier.cpp
+++ b/llvm/lib/IR/Verifier.cpp
@@ -1006,9 +1006,7 @@ void Verifier::visitGlobalIFunc(const GlobalIFunc &GI) {
   Check(isa<PointerType>(Resolver->getFunctionType()->getReturnType()),
         "IFunc resolver must return a pointer", &GI);
 
-  const Type *ResolverFuncTy =
-      GlobalIFunc::getResolverFunctionType(GI.getValueType());
-  Check(ResolverTy == ResolverFuncTy->getPointerTo(GI.getAddressSpace()),
+  Check(ResolverTy == PointerType::get(Context, GI.getAddressSpace()),
         "IFunc resolver has incorrect type", &GI);
 }
 

>From 1d4e02c36308c3250903e4ca86aaaec0e4831ab2 Mon Sep 17 00:00:00 2001
From: Bjorn Pettersson <bjorn.a.pettersson at ericsson.com>
Date: Fri, 9 Aug 2024 13:52:03 +0200
Subject: [PATCH 3/3] Clean up pointer casts etc after opaque pointers
 transition. NFC

---
 llvm/examples/BrainF/BrainFDriver.cpp         |  2 +-
 llvm/examples/ExceptionDemo/ExceptionDemo.cpp |  9 ++----
 llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp    |  4 +--
 .../InstCombine/InstCombineAndOrXor.cpp       |  2 +-
 .../InstCombine/InstCombineSelect.cpp         |  2 +-
 .../lib/Transforms/Scalar/NaryReassociate.cpp |  5 +--
 .../Utils/ScalarEvolutionExpanderTest.cpp     | 31 +++++++------------
 7 files changed, 21 insertions(+), 34 deletions(-)

diff --git a/llvm/examples/BrainF/BrainFDriver.cpp b/llvm/examples/BrainF/BrainFDriver.cpp
index 98fa735e1491f7..a42e67590533c9 100644
--- a/llvm/examples/BrainF/BrainFDriver.cpp
+++ b/llvm/examples/BrainF/BrainFDriver.cpp
@@ -72,7 +72,7 @@ void addMainFunction(Module *mod) {
   FunctionType *main_func_fty = FunctionType::get(
       Type::getInt32Ty(mod->getContext()),
       {Type::getInt32Ty(mod->getContext()),
-       Type::getInt8Ty(mod->getContext())->getPointerTo()->getPointerTo()},
+       PointerType::getUnqual(mod->getContext())},
       false);
   Function *main_func =
       Function::Create(main_func_fty, Function::ExternalLinkage, "main", mod);
diff --git a/llvm/examples/ExceptionDemo/ExceptionDemo.cpp b/llvm/examples/ExceptionDemo/ExceptionDemo.cpp
index fdee76cb96146e..27acb9a155ecd8 100644
--- a/llvm/examples/ExceptionDemo/ExceptionDemo.cpp
+++ b/llvm/examples/ExceptionDemo/ExceptionDemo.cpp
@@ -1244,8 +1244,7 @@ static llvm::Function *createCatchWrappedInvokeFunction(
   llvm::Value *unwindExceptionClass =
       builder.CreateLoad(builder.CreateStructGEP(
           ourUnwindExceptionType,
-          builder.CreatePointerCast(unwindException,
-                                    ourUnwindExceptionType->getPointerTo()),
+          unwindException,
           0));
 
   // Branch to the externalExceptionBlock if the exception is foreign or
@@ -1277,10 +1276,8 @@ static llvm::Function *createCatchWrappedInvokeFunction(
   // (OurException instance).
   //
   // Note: ourBaseFromUnwindOffset is usually negative
-  llvm::Value *typeInfoThrown = builder.CreatePointerCast(
-                                  builder.CreateConstGEP1_64(unwindException,
-                                                       ourBaseFromUnwindOffset),
-                                  ourExceptionType->getPointerTo());
+  llvm::Value *typeInfoThrown = builder.CreateConstGEP1_64(unwindException,
+                                                      ourBaseFromUnwindOffset));
 
   // Retrieve thrown exception type info type
   //
diff --git a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
index b64fe83959eb18..7ab2f2c13def87 100644
--- a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
@@ -2453,8 +2453,8 @@ bool AsmPrinter::doFinalization(Module &M) {
       auto SymbolName = "swift_async_extendedFramePointerFlags";
       auto Global = M.getGlobalVariable(SymbolName);
       if (!Global) {
-        auto Int8PtrTy = PointerType::getUnqual(M.getContext());
-        Global = new GlobalVariable(M, Int8PtrTy, false,
+        auto PtrTy = PointerType::getUnqual(M.getContext());
+        Global = new GlobalVariable(M, PtrTy, false,
                                     GlobalValue::ExternalWeakLinkage, nullptr,
                                     SymbolName);
         OutStreamer->emitSymbolAttribute(getSymbol(Global), MCSA_WeakReference);
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
index 2db05c669145b9..2bba83b5cde3c7 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
@@ -4567,7 +4567,7 @@ Instruction *InstCombinerImpl::foldNot(BinaryOperator &I) {
     Type *SextTy = cast<BitCastOperator>(NotOp)->getSrcTy();
     Value *NotX = Builder.CreateNot(X);
     Value *Sext = Builder.CreateSExt(NotX, SextTy);
-    return CastInst::CreateBitOrPointerCast(Sext, Ty);
+    return new BitCastInst(Sext, Ty);
   }
 
   if (auto *NotOpI = dyn_cast<Instruction>(NotOp))
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
index 6bcff6f9d49efd..b86f27168303a1 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
@@ -2386,7 +2386,7 @@ static Instruction *foldSelectCmpBitcasts(SelectInst &Sel,
   } else {
     return nullptr;
   }
-  return CastInst::CreateBitOrPointerCast(NewSel, Sel.getType());
+  return new BitCastInst(NewSel, Sel.getType());
 }
 
 /// Try to eliminate select instructions that test the returned flag of cmpxchg
diff --git a/llvm/lib/Transforms/Scalar/NaryReassociate.cpp b/llvm/lib/Transforms/Scalar/NaryReassociate.cpp
index c00c71fcb0b43a..39720672c202d7 100644
--- a/llvm/lib/Transforms/Scalar/NaryReassociate.cpp
+++ b/llvm/lib/Transforms/Scalar/NaryReassociate.cpp
@@ -421,10 +421,7 @@ NaryReassociatePass::tryReassociateGEPAtIndex(GetElementPtrInst *GEP,
     return nullptr;
 
   IRBuilder<> Builder(GEP);
-  // Candidate does not necessarily have the same pointer type as GEP. Use
-  // bitcast or pointer cast to make sure they have the same type, so that the
-  // later RAUW doesn't complain.
-  Candidate = Builder.CreateBitOrPointerCast(Candidate, GEP->getType());
+  // Candidate should have the same pointer type as GEP.
   assert(Candidate->getType() == GEP->getType());
 
   // NewGEP = (char *)Candidate + RHS * sizeof(IndexedType)
diff --git a/llvm/unittests/Transforms/Utils/ScalarEvolutionExpanderTest.cpp b/llvm/unittests/Transforms/Utils/ScalarEvolutionExpanderTest.cpp
index 32870d9fb37378..4d6111cc257c3b 100644
--- a/llvm/unittests/Transforms/Utils/ScalarEvolutionExpanderTest.cpp
+++ b/llvm/unittests/Transforms/Utils/ScalarEvolutionExpanderTest.cpp
@@ -73,9 +73,8 @@ TEST_F(ScalarEvolutionExpanderTest, ExpandPtrTypeSCEV) {
   // expansion when the value in ValueOffsetPair is a ptr and the offset
   // is not divisible by the elem type size of value.
   auto *I8Ty = Type::getInt8Ty(Context);
-  auto *I8PtrTy = PointerType::get(Context, 0);
+  auto *PtrTy = PointerType::get(Context, 0);
   auto *I32Ty = Type::getInt32Ty(Context);
-  auto *I32PtrTy = PointerType::get(Context, 0);
   FunctionType *FTy =
       FunctionType::get(Type::getVoidTy(Context), std::vector<Type *>(), false);
   Function *F = Function::Create(FTy, Function::ExternalLinkage, "f", M);
@@ -87,13 +86,11 @@ TEST_F(ScalarEvolutionExpanderTest, ExpandPtrTypeSCEV) {
 
   // loop:                            ; preds = %loop, %entry
   //   %alloca = alloca i32
-  //   %gep0 = getelementptr i32, i32* %alloca, i32 1
-  //   %bitcast1 = bitcast i32* %gep0 to i8*
-  //   %gep1 = getelementptr i8, i8* %bitcast1, i32 1
-  //   %gep2 = getelementptr i8, i8* undef, i32 1
-  //   %cmp = icmp ult i8* undef, %bitcast1
-  //   %select = select i1 %cmp, i8* %gep1, i8* %gep2
-  //   %bitcast2 = bitcast i8* %select to i32*
+  //   %gep0 = getelementptr i32, ptr %alloca, i32 1
+  //   %gep1 = getelementptr i8, ptr %gep0, i32 1
+  //   %gep2 = getelementptr i8, ptr undef, i32 1
+  //   %cmp = icmp ult ptr undef, %gep0
+  //   %select = select i1 %cmp, ptr %gep1, ptr %gep2
   //   br i1 undef, label %loop, label %exit
 
   const DataLayout &DL = F->getDataLayout();
@@ -102,24 +99,20 @@ TEST_F(ScalarEvolutionExpanderTest, ExpandPtrTypeSCEV) {
   AllocaInst *Alloca = new AllocaInst(I32Ty, DL.getAllocaAddrSpace(), "alloca",
                                       Br->getIterator());
   ConstantInt *Ci32 = ConstantInt::get(Context, APInt(32, 1));
+  UndefValue *UndefPtr = UndefValue::get(PtrTy);
   GetElementPtrInst *Gep0 =
       GetElementPtrInst::Create(I32Ty, Alloca, Ci32, "gep0", Br->getIterator());
-  CastInst *CastA = CastInst::CreateBitOrPointerCast(Gep0, I8PtrTy, "bitcast1",
-                                                     Br->getIterator());
   GetElementPtrInst *Gep1 =
-      GetElementPtrInst::Create(I8Ty, CastA, Ci32, "gep1", Br->getIterator());
+      GetElementPtrInst::Create(I8Ty, Gep0, Ci32, "gep1", Br->getIterator());
   GetElementPtrInst *Gep2 = GetElementPtrInst::Create(
-      I8Ty, UndefValue::get(I8PtrTy), Ci32, "gep2", Br->getIterator());
+      I8Ty, UndefPtr, Ci32, "gep2", Br->getIterator());
   CmpInst *Cmp = CmpInst::Create(Instruction::ICmp, CmpInst::ICMP_ULT,
-                                 UndefValue::get(I8PtrTy), CastA, "cmp",
-                                 Br->getIterator());
-  SelectInst *Sel =
+                                 UndefPtr, Gep0, "cmp", Br->getIterator());
+  SelectInst *Select =
       SelectInst::Create(Cmp, Gep1, Gep2, "select", Br->getIterator());
-  CastInst *CastB = CastInst::CreateBitOrPointerCast(Sel, I32PtrTy, "bitcast2",
-                                                     Br->getIterator());
 
   ScalarEvolution SE = buildSE(*F);
-  const SCEV *S = SE.getSCEV(CastB);
+  const SCEV *S = SE.getSCEV(Select);
   EXPECT_TRUE(isa<SCEVUnknown>(S));
 }
 



More information about the llvm-commits mailing list