|
19 | 19 | #include "llvm/ADT/DenseMap.h"
|
20 | 20 | #include "llvm/ADT/EquivalenceClasses.h"
|
21 | 21 | #include "llvm/ADT/PointerUnion.h"
|
| 22 | +#include "llvm/ADT/STLExtras.h" |
22 | 23 | #include "llvm/ADT/SetVector.h"
|
23 | 24 | #include "llvm/ADT/SmallVector.h"
|
24 | 25 | #include "llvm/ADT/Statistic.h"
|
25 | 26 | #include "llvm/ADT/StringRef.h"
|
26 | 27 | #include "llvm/ADT/TinyPtrVector.h"
|
| 28 | +#include "llvm/Analysis/LoopInfo.h" |
| 29 | +#include "llvm/Analysis/PostDominators.h" |
27 | 30 | #include "llvm/Analysis/TargetTransformInfo.h"
|
28 | 31 | #include "llvm/Analysis/TypeMetadataUtils.h"
|
29 | 32 | #include "llvm/Analysis/ValueTracking.h"
|
@@ -2472,3 +2475,94 @@ PreservedAnalyses LowerTypeTestsPass::run(Module &M,
|
2472 | 2475 | return PreservedAnalyses::all();
|
2473 | 2476 | return PreservedAnalyses::none();
|
2474 | 2477 | }
|
| 2478 | + |
| 2479 | +PreservedAnalyses SimplifyTypeTestsPass::run(Module &M, |
| 2480 | + ModuleAnalysisManager &AM) { |
| 2481 | + bool Changed = false; |
| 2482 | + // Figure out whether inlining has exposed a constant address to a lowered |
| 2483 | + // type test, and remove the test if so and the address is known to pass the |
| 2484 | + // test. Unfortunately this pass ends up needing to reverse engineer what |
| 2485 | + // LowerTypeTests did; this is currently inherent to the design of ThinLTO |
| 2486 | + // importing where LowerTypeTests needs to run at the start. |
| 2487 | + // |
| 2488 | + // We look for things like: |
| 2489 | + // |
| 2490 | + // sub (i64 ptrtoint (ptr @_Z2fpv to i64), i64 ptrtoint (ptr |
| 2491 | + // @__typeid__ZTSFvvE_global_addr to i64)) |
| 2492 | + // |
| 2493 | + // which gets replaced with 0 if _Z2fpv (more specifically _Z2fpv.cfi, the |
| 2494 | + // function referred to by the jump table) is a member of the type _ZTSFvv, as |
| 2495 | + // well as things like |
| 2496 | + // |
| 2497 | + // icmp eq ptr @_Z2fpv, @__typeid__ZTSFvvE_global_addr |
| 2498 | + // |
| 2499 | + // which gets replaced with true if _Z2fpv is a member. |
| 2500 | + for (auto &GV : M.globals()) { |
| 2501 | + if (!GV.getName().starts_with("__typeid_") || |
| 2502 | + !GV.getName().ends_with("_global_addr")) |
| 2503 | + continue; |
| 2504 | + // __typeid_foo_global_addr -> foo |
| 2505 | + auto *MD = MDString::get(M.getContext(), |
| 2506 | + GV.getName().substr(9, GV.getName().size() - 21)); |
| 2507 | + auto MaySimplifyPtr = [&](Value *Ptr) { |
| 2508 | + if (auto *GV = dyn_cast<GlobalValue>(Ptr)) |
| 2509 | + if (auto *CFIGV = M.getNamedValue((GV->getName() + ".cfi").str())) |
| 2510 | + Ptr = CFIGV; |
| 2511 | + return isKnownTypeIdMember(MD, M.getDataLayout(), Ptr, 0); |
| 2512 | + }; |
| 2513 | + auto MaySimplifyInt = [&](Value *Op) { |
| 2514 | + auto *PtrAsInt = dyn_cast<ConstantExpr>(Op); |
| 2515 | + if (!PtrAsInt || PtrAsInt->getOpcode() != Instruction::PtrToInt) |
| 2516 | + return false; |
| 2517 | + return MaySimplifyPtr(PtrAsInt->getOperand(0)); |
| 2518 | + }; |
| 2519 | + for (User *U : make_early_inc_range(GV.users())) { |
| 2520 | + if (auto *CI = dyn_cast<ICmpInst>(U)) { |
| 2521 | + if (CI->getPredicate() == CmpInst::ICMP_EQ && |
| 2522 | + MaySimplifyPtr(CI->getOperand(0))) { |
| 2523 | + // This is an equality comparison (TypeTestResolution::Single case in |
| 2524 | + // lowerTypeTestCall). In this case we just replace the comparison |
| 2525 | + // with true. |
| 2526 | + CI->replaceAllUsesWith(ConstantInt::getTrue(M.getContext())); |
| 2527 | + CI->eraseFromParent(); |
| 2528 | + Changed = true; |
| 2529 | + } |
| 2530 | + } |
| 2531 | + auto *CE = dyn_cast<ConstantExpr>(U); |
| 2532 | + if (!CE || CE->getOpcode() != Instruction::PtrToInt) |
| 2533 | + continue; |
| 2534 | + for (Use &U : make_early_inc_range(CE->uses())) { |
| 2535 | + auto *CE = dyn_cast<ConstantExpr>(U.getUser()); |
| 2536 | + if (U.getOperandNo() == 1 && CE && |
| 2537 | + CE->getOpcode() == Instruction::Sub && |
| 2538 | + MaySimplifyInt(CE->getOperand(0))) { |
| 2539 | + // This is a computation of PtrOffset as generated by |
| 2540 | + // LowerTypeTestsModule::lowerTypeTestCall above. If |
| 2541 | + // isKnownTypeIdMember passes we just pretend it evaluated to 0. This |
| 2542 | + // should cause later passes to remove the range and alignment checks. |
| 2543 | + // The bitset checks won't be removed but those are uncommon. |
| 2544 | + CE->replaceAllUsesWith(ConstantInt::get(CE->getType(), 0)); |
| 2545 | + Changed = true; |
| 2546 | + } |
| 2547 | + auto *CI = dyn_cast<ICmpInst>(U.getUser()); |
| 2548 | + if (U.getOperandNo() == 1 && CI && |
| 2549 | + CI->getPredicate() == CmpInst::ICMP_EQ && |
| 2550 | + MaySimplifyInt(CI->getOperand(0))) { |
| 2551 | + // This is an equality comparison. Unlike in the case above it |
| 2552 | + // remained as an integer compare. |
| 2553 | + CI->replaceAllUsesWith(ConstantInt::getTrue(M.getContext())); |
| 2554 | + CI->eraseFromParent(); |
| 2555 | + Changed = true; |
| 2556 | + } |
| 2557 | + } |
| 2558 | + } |
| 2559 | + } |
| 2560 | + |
| 2561 | + if (!Changed) |
| 2562 | + return PreservedAnalyses::all(); |
| 2563 | + PreservedAnalyses PA = PreservedAnalyses::none(); |
| 2564 | + PA.preserve<DominatorTreeAnalysis>(); |
| 2565 | + PA.preserve<PostDominatorTreeAnalysis>(); |
| 2566 | + PA.preserve<LoopAnalysis>(); |
| 2567 | + return PA; |
| 2568 | +} |
0 commit comments