1//===- LowerConstantIntrinsics.cpp - Lower constant intrinsic calls -------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This pass lowers all remaining 'objectsize' 'is.constant' intrinsic calls
10// and provides constant propagation and basic CFG cleanup on the result.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/Transforms/Scalar/LowerConstantIntrinsics.h"
15#include "llvm/ADT/PostOrderIterator.h"
16#include "llvm/ADT/SetVector.h"
17#include "llvm/ADT/Statistic.h"
18#include "llvm/Analysis/DomTreeUpdater.h"
19#include "llvm/Analysis/GlobalsModRef.h"
20#include "llvm/Analysis/InstructionSimplify.h"
21#include "llvm/Analysis/MemoryBuiltins.h"
22#include "llvm/Analysis/TargetLibraryInfo.h"
23#include "llvm/IR/BasicBlock.h"
24#include "llvm/IR/Constants.h"
25#include "llvm/IR/Dominators.h"
26#include "llvm/IR/Function.h"
27#include "llvm/IR/Instructions.h"
28#include "llvm/IR/IntrinsicInst.h"
29#include "llvm/IR/PatternMatch.h"
30#include "llvm/Pass.h"
31#include "llvm/Support/Debug.h"
32#include "llvm/Transforms/Scalar.h"
33#include "llvm/Transforms/Utils/Local.h"
34#include <optional>
35
36using namespace llvm;
37using namespace llvm::PatternMatch;
38
39#define DEBUG_TYPE "lower-is-constant-intrinsic"
40
41STATISTIC(IsConstantIntrinsicsHandled,
42 "Number of 'is.constant' intrinsic calls handled");
43STATISTIC(ObjectSizeIntrinsicsHandled,
44 "Number of 'objectsize' intrinsic calls handled");
45
46static Value *lowerIsConstantIntrinsic(IntrinsicInst *II) {
47 if (auto *C = dyn_cast<Constant>(Val: II->getOperand(i_nocapture: 0)))
48 if (C->isManifestConstant())
49 return ConstantInt::getTrue(Ty: II->getType());
50 return ConstantInt::getFalse(Ty: II->getType());
51}
52
53static bool replaceConditionalBranchesOnConstant(Instruction *II,
54 Value *NewValue,
55 DomTreeUpdater *DTU) {
56 bool HasDeadBlocks = false;
57 SmallSetVector<Instruction *, 8> UnsimplifiedUsers;
58 replaceAndRecursivelySimplify(I: II, SimpleV: NewValue, TLI: nullptr, DT: nullptr, AC: nullptr,
59 UnsimplifiedUsers: &UnsimplifiedUsers);
60 // UnsimplifiedUsers can contain PHI nodes that may be removed when
61 // replacing the branch instructions, so use a value handle worklist
62 // to handle those possibly removed instructions.
63 SmallVector<WeakVH, 8> Worklist(UnsimplifiedUsers.begin(),
64 UnsimplifiedUsers.end());
65
66 for (auto &VH : Worklist) {
67 CondBrInst *BI = dyn_cast_or_null<CondBrInst>(Val&: VH);
68 if (!BI)
69 continue;
70
71 BasicBlock *Target, *Other;
72 if (match(V: BI->getOperand(i_nocapture: 0), P: m_Zero())) {
73 Target = BI->getSuccessor(i: 1);
74 Other = BI->getSuccessor(i: 0);
75 } else if (match(V: BI->getOperand(i_nocapture: 0), P: m_One())) {
76 Target = BI->getSuccessor(i: 0);
77 Other = BI->getSuccessor(i: 1);
78 } else {
79 Target = nullptr;
80 Other = nullptr;
81 }
82 if (Target && Target != Other) {
83 BasicBlock *Source = BI->getParent();
84 Other->removePredecessor(Pred: Source);
85
86 Instruction *NewBI = UncondBrInst::Create(IfTrue: Target, InsertBefore: Source);
87 NewBI->setDebugLoc(BI->getDebugLoc());
88 BI->eraseFromParent();
89
90 if (DTU)
91 DTU->applyUpdates(Updates: {{DominatorTree::Delete, Source, Other}});
92 if (pred_empty(BB: Other))
93 HasDeadBlocks = true;
94 }
95 }
96 return HasDeadBlocks;
97}
98
99bool llvm::lowerConstantIntrinsics(Function &F, const TargetLibraryInfo &TLI,
100 DominatorTree *DT) {
101 std::optional<DomTreeUpdater> DTU;
102 if (DT)
103 DTU.emplace(args&: DT, args: DomTreeUpdater::UpdateStrategy::Lazy);
104
105 bool HasDeadBlocks = false;
106 const auto &DL = F.getDataLayout();
107 SmallVector<WeakTrackingVH, 8> Worklist;
108
109 ReversePostOrderTraversal<Function *> RPOT(&F);
110 for (BasicBlock *BB : RPOT) {
111 for (Instruction &I: *BB) {
112 IntrinsicInst *II = dyn_cast<IntrinsicInst>(Val: &I);
113 if (!II)
114 continue;
115 switch (II->getIntrinsicID()) {
116 default:
117 break;
118 case Intrinsic::is_constant:
119 case Intrinsic::objectsize:
120 Worklist.push_back(Elt: WeakTrackingVH(&I));
121 break;
122 }
123 }
124 }
125 for (WeakTrackingVH &VH: Worklist) {
126 // Items on the worklist can be mutated by earlier recursive replaces.
127 // This can remove the intrinsic as dead (VH == null), but also replace
128 // the intrinsic in place.
129 if (!VH)
130 continue;
131 IntrinsicInst *II = dyn_cast<IntrinsicInst>(Val: &*VH);
132 if (!II)
133 continue;
134 Value *NewValue;
135 switch (II->getIntrinsicID()) {
136 default:
137 continue;
138 case Intrinsic::is_constant:
139 NewValue = lowerIsConstantIntrinsic(II);
140 LLVM_DEBUG(dbgs() << "Folding " << *II << " to " << *NewValue << "\n");
141 IsConstantIntrinsicsHandled++;
142 break;
143 case Intrinsic::objectsize:
144 NewValue = lowerObjectSizeCall(ObjectSize: II, DL, TLI: &TLI, MustSucceed: true);
145 LLVM_DEBUG(dbgs() << "Folding " << *II << " to " << *NewValue << "\n");
146 ObjectSizeIntrinsicsHandled++;
147 break;
148 }
149 HasDeadBlocks |= replaceConditionalBranchesOnConstant(
150 II, NewValue, DTU: DTU ? &*DTU : nullptr);
151 }
152 if (HasDeadBlocks)
153 removeUnreachableBlocks(F, DTU: DTU ? &*DTU : nullptr);
154 return !Worklist.empty();
155}
156
157PreservedAnalyses
158LowerConstantIntrinsicsPass::run(Function &F, FunctionAnalysisManager &AM) {
159 if (lowerConstantIntrinsics(F, TLI: AM.getResult<TargetLibraryAnalysis>(IR&: F),
160 DT: AM.getCachedResult<DominatorTreeAnalysis>(IR&: F))) {
161 PreservedAnalyses PA;
162 PA.preserve<DominatorTreeAnalysis>();
163 return PA;
164 }
165
166 return PreservedAnalyses::all();
167}
168