11#include " LLVMPasses.h"
2- #include " llvm/IR/Instructions.h"
3- #include " llvm/Analysis/ValueTracking.h"
42#include " llvm/Analysis/TargetTransformInfo.h"
3+ #include " llvm/Analysis/ValueTracking.h"
54#include " llvm/IR/Dominators.h"
5+ #include " llvm/IR/Instructions.h"
66
77using namespace llvm ;
88
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- });
9+ static bool processPhiNode (PHINode *PhiNode , BasicBlock & BB) {
10+ if (! any_of (PhiNode-> incoming_values (), [](Use &U) {
11+ if (Constant *C = dyn_cast<Constant>(&U)) {
12+ return C-> isNullValue ();
13+ }
14+ return false ;
15+ })) {
16+ return false ;
17+ }
1818
1919 bool Changed = false ;
20- if (phiHasNullValue ) {
21- for (Instruction &I : BB) {
22- if (I. getOpcode () == Instruction::SDiv || I.getOpcode () == Instruction::SRem) {
23- const size_t OpIdx = 1 ; // I.getOpcode() == Instruction::SRem ? 0 : 1;
24- if (I.getOperand (OpIdx) == phiNode ) {
25- auto *freezePhi = new FreezeInst (phiNode, phiNode-> getName () + " .frozen " , I. getIterator ());
26- I. setOperand (OpIdx, freezePhi );
27- Changed = true ;
28- }
20+ for (Instruction &I : BB ) {
21+ if (I. getOpcode () == Instruction::SDiv ||
22+ I.getOpcode () == Instruction::SRem) {
23+ const size_t OpIdx = 1 ;
24+ if (I.getOperand (OpIdx) == PhiNode ) {
25+ auto *freezePhi = new FreezeInst (
26+ PhiNode, PhiNode-> getName () + " .frozen " , I. getIterator () );
27+ I. setOperand (OpIdx, freezePhi) ;
28+ Changed = true ;
2929 }
3030 }
31- #if 0
32- auto FindUse = llvm::find_if(phiNode->users(), [](auto *U) {
33- auto *Use = cast<Instruction>(U);
34- llvm::errs() << "User: " << *Use << "\n";
35- return (Use->getOpcode() == Instruction::SDiv || Use->getOpcode() == Instruction::SRem);
36- });
37- if (FindUse == phiNode->user_end()) {
38- llvm::errs() << "no div :(\n";
39- return false;
40- }
41- auto *Use = cast<Instruction>(*FindUse);
42- assert(Use->isIntDivRem());
43- const size_t OpIdx = Use->getOpcode() == Instruction::SRem ? 0 : 1;
44- if (Use->getOperand(OpIdx) == phiNode) {
45- llvm::errs() << "Got our user! " << *Use << "\n";
46- llvm::errs() << "Operand 1: " << *Use->getOperand(1) << "\n";
47- auto *freezePhi = new FreezeInst(phiNode, phiNode->getName() + ".frozen", Use->getIterator());
48- Use->setOperand(OpIdx, freezePhi);
49- Changed = true;
50- }
51- #endif
5231 }
53- return Changed;
32+ return Changed;
5433}
5534
56- static bool runOnFunction (Function& F, const TargetTransformInfo &TTI,
57- const DominatorTree &DT) {
58- bool Changed = false ;
35+ static bool runOnFunction (Function &F) {
36+ bool Changed = false ;
5937
60- SmallVector<PHINode *> PhiNodes;
61- for (BasicBlock &BB : F) {
62- for (Instruction &inst : BB) {
63- if (PHINode *phiNode = dyn_cast<PHINode>(&inst )) {
64- Changed |= processPhiNode (phiNode , BB);
38+ SmallVector<PHINode *> PhiNodes;
39+ for (BasicBlock &BB : F) {
40+ for (Instruction &I : BB) {
41+ if (PHINode *PhiNode = dyn_cast<PHINode>(&I )) {
42+ Changed |= processPhiNode (PhiNode , BB);
6543 continue ;
6644 }
6745 break ;
6846 }
6947 }
7048
71- return Changed;
49+ return Changed;
7250}
7351
74- PreservedAnalyses FreezeMaskedDivRemPass::run (Function &F, FunctionAnalysisManager &FAM) {
75- TargetTransformInfo &TTI = FAM.getResult <TargetIRAnalysis>(F);
76- DominatorTree &DT = FAM.getResult <DominatorTreeAnalysis>(F);
77- const auto b = runOnFunction (F, TTI, DT);
52+ PreservedAnalyses FreezeMaskedDivRemPass::run (Function &F,
53+ FunctionAnalysisManager &FAM) {
54+ const auto b = runOnFunction (F);
7855
79- return b ? PreservedAnalyses::none () : PreservedAnalyses::all ();
80- }
56+ return b ? PreservedAnalyses::none () : PreservedAnalyses::all ();
57+ }
0 commit comments