llvm.org GIT mirror llvm / 95ba981
Revert r337904: [IPSCCP] Use PredicateInfo to propagate facts from cmp instructions. I suspect it is causing the clang-stage2-Rthinlto failures. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@337956 91177308-0d34-0410-b5e6-96231b3b80d8 Florian Hahn 1 year, 1 month ago
8 changed file(s) with 15 addition(s) and 161 deletion(s). Raw diff Collapse all Expand all
2020 #ifndef LLVM_TRANSFORMS_SCALAR_SCCP_H
2121 #define LLVM_TRANSFORMS_SCALAR_SCCP_H
2222
23 #include "llvm/ADT/STLExtras.h"
2423 #include "llvm/Analysis/TargetLibraryInfo.h"
2524 #include "llvm/IR/DataLayout.h"
2625 #include "llvm/IR/Function.h"
2726 #include "llvm/IR/Module.h"
2827 #include "llvm/IR/PassManager.h"
29 #include "llvm/Transforms/Utils/PredicateInfo.h"
3028
3129 namespace llvm {
3230
3836 PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
3937 };
4038
41 bool runIPSCCP(
42 Module &M, const DataLayout &DL, const TargetLibraryInfo *TLI,
43 function_ref(Function &)> getPredicateInfo);
39 bool runIPSCCP(Module &M, const DataLayout &DL, const TargetLibraryInfo *TLI);
4440 } // end namespace llvm
4541
4642 #endif // LLVM_TRANSFORMS_SCALAR_SCCP_H
0 #include "llvm/Transforms/IPO/SCCP.h"
1 #include "llvm/Analysis/AssumptionCache.h"
21 #include "llvm/Analysis/TargetLibraryInfo.h"
32 #include "llvm/Transforms/IPO.h"
43 #include "llvm/Transforms/Scalar/SCCP.h"
87 PreservedAnalyses IPSCCPPass::run(Module &M, ModuleAnalysisManager &AM) {
98 const DataLayout &DL = M.getDataLayout();
109 auto &TLI = AM.getResult(M);
11 auto &FAM = AM.getResult(M).getManager();
12 auto getPredicateInfo =
13 [&FAM](Function &F) -> std::unique_ptr {
14 return make_unique(F,
15 FAM.getResult(F),
16 FAM.getResult(F));
17 };
18
19 if (!runIPSCCP(M, DL, &TLI, getPredicateInfo))
10 if (!runIPSCCP(M, DL, &TLI))
2011 return PreservedAnalyses::all();
2112 return PreservedAnalyses::none();
2213 }
4233 const DataLayout &DL = M.getDataLayout();
4334 const TargetLibraryInfo *TLI =
4435 &getAnalysis().getTLI();
45
46 auto getPredicateInfo =
47 [this](Function &F) -> std::unique_ptr {
48 return make_unique(
49 F, this->getAnalysis(F).getDomTree(),
50 this->getAnalysis().getAssumptionCache(F));
51 };
52
53 return runIPSCCP(M, DL, TLI, getPredicateInfo);
36 return runIPSCCP(M, DL, TLI);
5437 }
5538
5639 void getAnalysisUsage(AnalysisUsage &AU) const override {
57 AU.addRequired();
58 AU.addRequired();
5940 AU.addRequired();
6041 }
6142 };
6748 INITIALIZE_PASS_BEGIN(IPSCCPLegacyPass, "ipsccp",
6849 "Interprocedural Sparse Conditional Constant Propagation",
6950 false, false)
70 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
7151 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
7252 INITIALIZE_PASS_END(IPSCCPLegacyPass, "ipsccp",
7353 "Interprocedural Sparse Conditional Constant Propagation",
5454 #include "llvm/Support/ErrorHandling.h"
5555 #include "llvm/Support/raw_ostream.h"
5656 #include "llvm/Transforms/Scalar.h"
57 #include "llvm/Transforms/Utils/PredicateInfo.h"
5857 #include
5958 #include
6059 #include
246245 using Edge = std::pair;
247246 DenseSet KnownFeasibleEdges;
248247
249 DenseMap> PredInfos;
250 DenseMap> AdditionalUsers;
251
252248 public:
253 void addPredInfo(Function &F, std::unique_ptr PI) {
254 PredInfos[&F] = std::move(PI);
255 }
256
257 const PredicateBase *getPredicateInfoFor(Instruction *I) {
258 auto PI = PredInfos.find(I->getFunction());
259 if (PI == PredInfos.end())
260 return nullptr;
261 return PI->second->getPredicateInfoFor(I);
262 }
263
264249 SCCPSolver(const DataLayout &DL, const TargetLibraryInfo *tli)
265250 : DL(DL), TLI(tli) {}
266251
570555 void OperandChangedState(Instruction *I) {
571556 if (BBExecutable.count(I->getParent())) // Inst is executable?
572557 visit(*I);
573 }
574
575 // Add U as additional user of V.
576 void addAdditionalUser(Value *V, User *U) {
577 auto Iter = AdditionalUsers.insert({V, {}});
578 Iter.first->second.insert(U);
579 }
580
581 // Mark I's users as changed, including AdditionalUsers.
582 void markUsersAsChanged(Value *I) {
583 for (User *U : I->users())
584 if (auto *UI = dyn_cast(U))
585 OperandChangedState(UI);
586
587 auto Iter = AdditionalUsers.find(I);
588 if (Iter != AdditionalUsers.end()) {
589 for (User *U : Iter->second)
590 if (auto *UI = dyn_cast(U))
591 OperandChangedState(UI);
592 }
593558 }
594559
595560 private:
11531118 Function *F = CS.getCalledFunction();
11541119 Instruction *I = CS.getInstruction();
11551120
1156 if (auto *II = dyn_cast(I)) {
1157 if (II->getIntrinsicID() == Intrinsic::ssa_copy) {
1158 if (ValueState[I].isOverdefined())
1159 return;
1160
1161 auto *PI = getPredicateInfoFor(I);
1162 if (!PI)
1163 return;
1164
1165 auto *PBranch = dyn_cast(getPredicateInfoFor(I));
1166 if (!PBranch) {
1167 mergeInValue(ValueState[I], I, getValueState(PI->OriginalOp));
1168 return;
1169 }
1170
1171 Value *CopyOf = I->getOperand(0);
1172 Value *Cond = PBranch->Condition;
1173
1174 // Everything below relies on the condition being a comparison.
1175 auto *Cmp = dyn_cast(Cond);
1176 if (!Cmp) {
1177 mergeInValue(ValueState[I], I, getValueState(PI->OriginalOp));
1178 return;
1179 }
1180
1181 Value *CmpOp0 = Cmp->getOperand(0);
1182 Value *CmpOp1 = Cmp->getOperand(1);
1183 if (CopyOf != CmpOp0 && CopyOf != CmpOp1) {
1184 mergeInValue(ValueState[I], I, getValueState(PI->OriginalOp));
1185 return;
1186 }
1187
1188 if (CmpOp0 != CopyOf)
1189 std::swap(CmpOp0, CmpOp1);
1190
1191 LatticeVal OriginalVal = getValueState(CopyOf);
1192 LatticeVal EqVal = getValueState(CmpOp1);
1193 LatticeVal &IV = ValueState[I];
1194 if (PBranch->TrueEdge && Cmp->getPredicate() == CmpInst::ICMP_EQ) {
1195 addAdditionalUser(CmpOp1, I);
1196 if (OriginalVal.isConstant())
1197 mergeInValue(IV, I, OriginalVal);
1198 else
1199 mergeInValue(IV, I, EqVal);
1200 return;
1201 }
1202 if (!PBranch->TrueEdge && Cmp->getPredicate() == CmpInst::ICMP_NE) {
1203 addAdditionalUser(CmpOp1, I);
1204 if (OriginalVal.isConstant())
1205 mergeInValue(IV, I, OriginalVal);
1206 else
1207 mergeInValue(IV, I, EqVal);
1208 return;
1209 }
1210
1211 return (void)mergeInValue(IV, I, getValueState(PBranch->OriginalOp));
1212 }
1213 }
1214
12151121 // The common case is that we aren't tracking the callee, either because we
12161122 // are not doing interprocedural analysis or the callee is indirect, or is
12171123 // external. Handle these cases first.
13311237 // since all of its users will have already been marked as overdefined
13321238 // Update all of the users of this instruction's value.
13331239 //
1334 markUsersAsChanged(I);
1240 for (User *U : I->users())
1241 if (auto *UI = dyn_cast(U))
1242 OperandChangedState(UI);
13351243 }
13361244
13371245 // Process the instruction work list.
13481256 // Update all of the users of this instruction's value.
13491257 //
13501258 if (I->getType()->isStructTy() || !getValueState(I).isOverdefined())
1351 markUsersAsChanged(I);
1259 for (User *U : I->users())
1260 if (auto *UI = dyn_cast(U))
1261 OperandChangedState(UI);
13521262 }
13531263
13541264 // Process the basic block work list.
18871797 }
18881798 }
18891799
1890 bool llvm::runIPSCCP(
1891 Module &M, const DataLayout &DL, const TargetLibraryInfo *TLI,
1892 function_ref(Function &)> getPredicateInfo) {
1800 bool llvm::runIPSCCP(Module &M, const DataLayout &DL,
1801 const TargetLibraryInfo *TLI) {
18931802 SCCPSolver Solver(DL, TLI);
18941803
18951804 // Loop over all functions, marking arguments to those with their addresses
18981807 if (F.isDeclaration())
18991808 continue;
19001809
1901 Solver.addPredInfo(F, getPredicateInfo(F));
19021810 // Determine if we can track the function's return values. If so, add the
19031811 // function to the solver's set of return-tracked functions.
19041812 if (canTrackReturnsInterprocedurally(&F))
20511959 F.getBasicBlockList().erase(DeadBB);
20521960 }
20531961 BlocksToErase.clear();
2054
2055 for (BasicBlock &BB : F) {
2056 for (BasicBlock::iterator BI = BB.begin(), E = BB.end(); BI != E;) {
2057 Instruction *Inst = &*BI++;
2058 if (const PredicateBase *PI = Solver.getPredicateInfoFor(Inst)) {
2059 if (auto *II = dyn_cast(Inst)) {
2060 if (II->getIntrinsicID() == Intrinsic::ssa_copy) {
2061 Value *Op = II->getOperand(0);
2062 Inst->replaceAllUsesWith(Op);
2063 Inst->eraseFromParent();
2064 continue;
2065 }
2066 }
2067 Inst->replaceAllUsesWith(PI->OriginalOp);
2068 Inst->eraseFromParent();
2069 }
2070 }
2071 }
20721962 }
20731963
20741964 // If we inferred constant or undef return values for a function, we replaced
4040 ; CHECK-O2-NEXT: Running analysis: ProfileSummaryAnalysis
4141 ; CHECK-O2-NEXT: Running analysis: OptimizationRemarkEmitterAnalysis
4242 ; CHECK-O2-NEXT: Running pass: IPSCCPPass
43 ; CHECK-O2-DAG: Running analysis: AssumptionAnalysis on foo
44 ; CHECK-O2-DAG: Running analysis: DominatorTreeAnalysis on foo
4543 ; CHECK-O2-NEXT: Running pass: CalledValuePropagationPass
4644 ; CHECK-O-NEXT: Running pass: ModuleToPostOrderCGSCCPassAdaptor<{{.*}}PostOrderFunctionAttrsPass>
4745 ; CHECK-O-NEXT: Running analysis: InnerAnalysisManagerProxy<{{.*}}SCC
5856 ; CHECK-O1-NEXT: Running pass: LowerTypeTestsPass
5957 ; CHECK-O2-NEXT: Running pass: GlobalOptPass
6058 ; CHECK-O2-NEXT: Running pass: ModuleToFunctionPassAdaptor<{{.*}}PromotePass>
59 ; CHECK-O2-NEXT: Running analysis: DominatorTreeAnalysis
60 ; CHECK-O2-NEXT: Running analysis: AssumptionAnalysis
6161 ; CHECK-O2-NEXT: Running pass: ConstantMergePass
6262 ; CHECK-O2-NEXT: Running pass: DeadArgumentEliminationPass
6363 ; CHECK-O2-NEXT: Running pass: ModuleToFunctionPassAdaptor<{{.*}}PassManager{{.*}}>
2727 ; CHECK-NEXT: Force set function attributes
2828 ; CHECK-NEXT: Infer set function attributes
2929 ; CHECK-NEXT: Interprocedural Sparse Conditional Constant Propagation
30 ; CHECK-NEXT: Unnamed pass: implement Pass::getPassName()
3130 ; CHECK-NEXT: Called Value Propagation
3231 ; CHECK-NEXT: Global Variable Optimizer
3332 ; CHECK-NEXT: Unnamed pass: implement Pass::getPassName()
270269 ; CHECK-NEXT: Module Verifier
271270 ; CHECK-NEXT: Bitcode Writer
272271 ; CHECK-NEXT: Pass Arguments:
273 ; CHECK-NEXT: FunctionPass Manager
274 ; CHECK-NEXT: Dominator Tree Construction
275 ; CHECK-NEXT: Pass Arguments:
276272 ; CHECK-NEXT: Target Library Information
277273 ; CHECK-NEXT: FunctionPass Manager
278274 ; CHECK-NEXT: Dominator Tree Construction
2929 ; CHECK-NEXT: FunctionPass Manager
3030 ; CHECK-NEXT: Call-site splitting
3131 ; CHECK-NEXT: Interprocedural Sparse Conditional Constant Propagation
32 ; CHECK-NEXT: Unnamed pass: implement Pass::getPassName()
3332 ; CHECK-NEXT: Called Value Propagation
3433 ; CHECK-NEXT: Global Variable Optimizer
3534 ; CHECK-NEXT: Unnamed pass: implement Pass::getPassName()
274273 ; CHECK-NEXT: Module Verifier
275274 ; CHECK-NEXT: Bitcode Writer
276275 ; CHECK-NEXT: Pass Arguments:
277 ; CHECK-NEXT: FunctionPass Manager
278 ; CHECK-NEXT: Dominator Tree Construction
279 ; CHECK-NEXT: Pass Arguments:
280276 ; CHECK-NEXT: Target Library Information
281277 ; CHECK-NEXT: FunctionPass Manager
282278 ; CHECK-NEXT: Dominator Tree Construction
2727 ; CHECK-NEXT: Force set function attributes
2828 ; CHECK-NEXT: Infer set function attributes
2929 ; CHECK-NEXT: Interprocedural Sparse Conditional Constant Propagation
30 ; CHECK-NEXT: Unnamed pass: implement Pass::getPassName()
3130 ; CHECK-NEXT: Called Value Propagation
3231 ; CHECK-NEXT: Global Variable Optimizer
3332 ; CHECK-NEXT: Unnamed pass: implement Pass::getPassName()
257256 ; CHECK-NEXT: Module Verifier
258257 ; CHECK-NEXT: Bitcode Writer
259258 ; CHECK-NEXT: Pass Arguments:
260 ; CHECK-NEXT: FunctionPass Manager
261 ; CHECK-NEXT: Dominator Tree Construction
262 ; CHECK-NEXT: Pass Arguments:
263259 ; CHECK-NEXT: Target Library Information
264260 ; CHECK-NEXT: FunctionPass Manager
265261 ; CHECK-NEXT: Dominator Tree Construction
88 %c1 = icmp eq i8 %v, 0
99 br i1 %c1, label %true, label %false
1010 true:
11 ; CHECK: %ca = musttail call i8* @side_effects(i8 0)
11 ; CHECK: %ca = musttail call i8* @side_effects(i8 %v)
1212 ; CHECK: ret i8* %ca
1313 %ca = musttail call i8* @side_effects(i8 %v)
1414 ret i8* %ca
3333 ; is always `null`.
3434 ; The call can't be removed due to `external` call above, though.
3535
36 ; CHECK: %ca = musttail call i8* @start(i8 0)
36 ; CHECK: %ca = musttail call i8* @start(i8 %v)
3737 %ca = musttail call i8* @start(i8 %v)
3838
3939 ; Thus the result must be returned anyway