1+ #include " LLVMPasses.h"
2+ #include " llvm/IR/Instructions.h"
3+ #include " llvm/Analysis/ValueTracking.h"
4+ #include " llvm/Analysis/TargetTransformInfo.h"
5+ #include " llvm/IR/Dominators.h"
6+
7+ using namespace llvm ;
8+
9+ static bool processPhiNode (PHINode *phiNode, BasicBlock& BB) {
10+ llvm::errs () << " YOLO: " << *phiNode << " \n " ;
11+
12+ const auto phiHasNullValue = any_of (phiNode->incoming_values (), [](Use& U) {
13+ if (Constant *C = dyn_cast<Constant>(&U)) {
14+ return C->isNullValue ();
15+ }
16+ return false ;
17+ });
18+
19+ if (phiHasNullValue) {
20+ auto FindUse = llvm::find_if (phiNode->users (), [](auto *U) {
21+ auto *Use = cast<Instruction>(U);
22+ llvm::errs () << " User: " << *Use << " \n " ;
23+ return (Use->getOpcode () == Instruction::SDiv);
24+ });
25+ if (FindUse == phiNode->user_end ()) {
26+ llvm::errs () << " no div :(\n " ;
27+ return false ;
28+ }
29+ auto *Use = cast<Instruction>(*FindUse);
30+ assert ()
31+ llvm::errs () << " Got our user! " << *Use << " \n " ;
32+
33+ // insert freeze between phi and sdiv
34+ //
35+ }
36+ return false ;
37+ }
38+
39+ static bool runOnFunction (Function& F, const TargetTransformInfo &TTI,
40+ const DominatorTree &DT) {
41+ bool Changed = false ;
42+
43+ SmallVector<PHINode *> PhiNodes;
44+ for (BasicBlock &BB : F) {
45+ for (Instruction &inst : BB) {
46+ if (PHINode *phiNode = dyn_cast<PHINode>(&inst)) {
47+ Changed |= processPhiNode (phiNode, BB);
48+ continue ;
49+ }
50+ break ;
51+ }
52+ }
53+
54+ return Changed;
55+ }
56+
57+ PreservedAnalyses FreezeMaskedDivRemPass::run (Function &F, FunctionAnalysisManager &FAM) {
58+ TargetTransformInfo &TTI = FAM.getResult <TargetIRAnalysis>(F);
59+ DominatorTree &DT = FAM.getResult <DominatorTreeAnalysis>(F);
60+ const auto b = runOnFunction (F, TTI, DT);
61+
62+ return b ? PreservedAnalyses::none () : PreservedAnalyses::all ();
63+ }
0 commit comments