LLVM 24.0.0git
BPFASpaceCastSimplifyPass.cpp
Go to the documentation of this file.
1//===-- BPFASpaceCastSimplifyPass.cpp - BPF addrspacecast simplications --===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "BPF.h"
10#include "llvm/IR/ValueHandle.h"
11#include <optional>
12
13#define DEBUG_TYPE "bpf-aspace-simplify"
14
15using namespace llvm;
16
17namespace {
18
19struct CastGEPCast {
20 AddrSpaceCastInst *OuterCast;
21
22 // Match chain of instructions:
23 // %inner = addrspacecast N->M
24 // %gep = getelementptr %inner, ...
25 // %outer = addrspacecast M->N %gep
26 // Where I is %outer.
27 static std::optional<CastGEPCast> match(Value *I) {
28 auto *OuterCast = dyn_cast<AddrSpaceCastInst>(I);
29 if (!OuterCast)
30 return std::nullopt;
32 if (!GEP)
33 return std::nullopt;
34 auto *InnerCast = dyn_cast<AddrSpaceCastInst>(GEP->getPointerOperand());
35 if (!InnerCast)
36 return std::nullopt;
37 if (InnerCast->getSrcAddressSpace() != OuterCast->getDestAddressSpace())
38 return std::nullopt;
39 if (InnerCast->getDestAddressSpace() != OuterCast->getSrcAddressSpace())
40 return std::nullopt;
41 return CastGEPCast{OuterCast};
42 }
43
44 static PointerType *changeAddressSpace(PointerType *Ty, unsigned AS) {
45 return Ty->get(Ty->getContext(), AS);
46 }
47
48 // Assuming match(this->OuterCast) is true, convert:
49 // (addrspacecast M->N (getelementptr (addrspacecast N->M ptr) ...))
50 // To:
51 // (getelementptr ptr ...)
52 GetElementPtrInst *rewrite() {
53 auto *GEP = cast<GetElementPtrInst>(OuterCast->getPointerOperand());
54 auto *InnerCast = cast<AddrSpaceCastInst>(GEP->getPointerOperand());
55 unsigned AS = OuterCast->getDestAddressSpace();
56 auto *NewGEP = cast<GetElementPtrInst>(GEP->clone());
57 NewGEP->setName(GEP->getName());
58 NewGEP->insertAfter(OuterCast->getIterator());
59 NewGEP->setOperand(0, InnerCast->getPointerOperand());
60 auto *GEPTy = cast<PointerType>(GEP->getType());
61 NewGEP->mutateType(changeAddressSpace(GEPTy, AS));
62 OuterCast->replaceAllUsesWith(NewGEP);
63 OuterCast->eraseFromParent();
64 if (GEP->use_empty())
65 GEP->eraseFromParent();
66 if (InnerCast->use_empty())
67 InnerCast->eraseFromParent();
68 return NewGEP;
69 }
70};
71
72} // anonymous namespace
73
77 bool Changed = false;
78 for (BasicBlock &BB : F) {
79 for (Instruction &I : BB)
80 if (CastGEPCast::match(&I))
81 WorkList.push_back(&I);
82
83 while (!WorkList.empty()) {
84 Value *V = WorkList.pop_back_val();
85 if (!V)
86 continue;
87 auto InsnChain = CastGEPCast::match(V);
88 if (!InsnChain)
89 continue;
90 GetElementPtrInst *NewGEP = InsnChain->rewrite();
91 Changed = true;
92 for (User *U : NewGEP->users())
93 if (CastGEPCast::match(U))
94 WorkList.push_back(U);
95 }
96 }
98}
Hexagon Common GEP
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
This class represents a conversion between pointers from one address space to another.
Value * getPointerOperand()
Gets the pointer operand.
unsigned getSrcAddressSpace() const
Returns the address space of the pointer operand.
unsigned getDestAddressSpace() const
Returns the address space of the result.
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
LLVM Basic Block Representation.
Definition BasicBlock.h:62
an instruction for type-safe pointer arithmetic to access elements of arrays and structs
LLVM_ABI InstListType::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
iterator_range< user_iterator > users()
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Definition Analysis.h:115
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
LLVM Value Representation.
Definition Value.h:75
LLVM_ABI void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition Value.cpp:548
self_iterator getIterator()
Definition ilist_node.h:123
bool match(Val *V, const Pattern &P)
This is an optimization pass for GlobalISel generic memory operations.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.