24#define DEBUG_TYPE "spirv-regularizer"
36 StringRef getPassName()
const override {
return "SPIR-V Regularizer"; }
38 void getAnalysisUsage(AnalysisUsage &AU)
const override {
39 FunctionPass::getAnalysisUsage(AU);
44char SPIRVRegularizerLegacy::ID = 0;
57static
void runLowerConstExpr(
Function &
F) {
59 std::list<Instruction *> WorkList;
61 WorkList.push_back(&
II);
63 auto FBegin =
F.begin();
64 while (!WorkList.empty()) {
65 Instruction *II = WorkList.front();
67 auto LowerOp = [&II, &FBegin, &F](Value *V) -> Value * {
70 auto *CE = cast<ConstantExpr>(V);
71 LLVM_DEBUG(dbgs() <<
"[lowerConstantExpressions] " << *CE);
72 auto ReplInst = CE->getAsInstruction();
73 auto InsPoint = II->getParent() == &*FBegin ? II : &FBegin->back();
74 ReplInst->insertBefore(InsPoint->getIterator());
75 LLVM_DEBUG(dbgs() <<
" -> " << *ReplInst <<
'\n');
76 std::vector<Instruction *> Users;
78 for (auto U : CE->users()) {
79 LLVM_DEBUG(dbgs() <<
"[lowerConstantExpressions] Use: " << *U <<
'\n');
80 auto InstUser = dyn_cast<Instruction>(U);
82 if (InstUser && InstUser->getParent()->getParent() == &F)
83 Users.push_back(InstUser);
85 for (auto &User : Users) {
86 if (ReplInst->getParent() == User->getParent() &&
87 User->comesBefore(ReplInst))
88 ReplInst->moveBefore(User->getIterator());
89 User->replaceUsesOfWith(CE, ReplInst);
95 auto LowerConstantVec = [&
II, &LowerOp, &WorkList,
97 unsigned NumOfOp) ->
Value * {
98 if (llvm::all_of(Vec->operands(), [](Value *V) {
99 return isa<ConstantExpr>(V) || isa<Function>(V);
103 std::list<Value *> OpList;
105 [LowerOp](
Value *V) { return LowerOp(V); });
106 Value *Repl =
nullptr;
110 PhiII ? &PhiII->getIncomingBlock(NumOfOp)->back() :
II;
111 std::list<Instruction *> ReplList;
112 for (
auto V : OpList) {
114 ReplList.push_back(Inst);
120 WorkList.splice(WorkList.begin(), ReplList);
125 for (
unsigned OI = 0, OE =
II->getNumOperands(); OI != OE; ++OI) {
126 auto *
Op =
II->getOperand(OI);
128 Value *ReplInst = LowerConstantVec(Vec, OI);
130 II->replaceUsesOfWith(
Op, ReplInst);
138 Value *ReplInst =
nullptr;
140 ReplInst = LowerConstantVec(Vec, OI);
142 ReplInst = LowerOp(CE);
147 II->setOperand(OI, RepMDVal);
164 bool IsI1 = Cmp->getOperand(0)->getType()->getScalarType()->isIntegerTy(1);
168 auto Pred = Cmp->getPredicate();
174 Value *
P = Cmp->getOperand(0);
175 Value *Q = Cmp->getOperand(1);
178 Value *Result =
nullptr;
183 Result = Builder.CreateAnd(
P, Builder.CreateNot(Q));
188 Result = Builder.CreateAnd(Q, Builder.CreateNot(
P));
193 Result = Builder.CreateOr(Q, Builder.CreateNot(
P));
198 Result = Builder.CreateOr(
P, Builder.CreateNot(Q));
204 Result->takeName(Cmp);
205 Cmp->replaceAllUsesWith(Result);
206 Cmp->eraseFromParent();
212 runLowerConstExpr(
F);
222 return new SPIRVRegularizerLegacy();
Expand Atomic instructions
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
static bool runImpl(MachineFunction &MF)
This file contains the declarations for the subclasses of Constant, which represent the different fla...
static bool runOnFunction(Function &F, bool PostInlining)
This header defines various interfaces for pass management in LLVM.
uint64_t IntrinsicInst * II
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
static void runLowerI1Comparisons(Function &F)
static bool runImpl(Function &F)
@ ICMP_SLT
signed less than
@ ICMP_SLE
signed less or equal
@ ICMP_UGE
unsigned greater or equal
@ ICMP_UGT
unsigned greater than
@ ICMP_SGT
signed greater than
@ ICMP_ULT
unsigned less than
@ ICMP_SGE
signed greater or equal
@ ICMP_ULE
unsigned less or equal
Constant Vector Declarations.
FixedVectorType * getType() const
Specialize the getType() method to always return a FixedVectorType, which reduces the amount of casti...
This is an important base class in LLVM.
FunctionPass class - This class is used to implement most global optimizations.
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
static InsertElementInst * Create(Value *Vec, Value *NewElt, Value *Idx, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
This is an important class for using LLVM in a threaded context.
static LLVM_ABI PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
A set of analyses that are preserved following a run of a transformation pass.
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
static LLVM_ABI IntegerType * getInt32Ty(LLVMContext &C)
LLVM Value Representation.
self_iterator getIterator()
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
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.
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
OutputIt transform(R &&Range, OutputIt d_first, UnaryFunction F)
Wrapper function around std::transform to apply a function to a range and store the result elsewhere.
FunctionPass * createSPIRVRegularizerPass()
DWARFExpression::Operation Op
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.