1 //===- LowerConstantIntrinsics.cpp - Lower constant intrinsic calls -------===//
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
7 //===----------------------------------------------------------------------===//
9 // This pass lowers all remaining 'objectsize' 'is.constant' intrinsic calls
10 // and provides constant propagation and basic CFG cleanup on the result.
12 //===----------------------------------------------------------------------===//
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/Intrinsics.h"
30 #include "llvm/IR/PatternMatch.h"
31 #include "llvm/InitializePasses.h"
32 #include "llvm/Pass.h"
33 #include "llvm/Support/Debug.h"
34 #include "llvm/Transforms/Scalar.h"
35 #include "llvm/Transforms/Utils/Local.h"
38 using namespace llvm::PatternMatch
;
40 #define DEBUG_TYPE "lower-is-constant-intrinsic"
42 STATISTIC(IsConstantIntrinsicsHandled
,
43 "Number of 'is.constant' intrinsic calls handled");
44 STATISTIC(ObjectSizeIntrinsicsHandled
,
45 "Number of 'objectsize' intrinsic calls handled");
47 static Value
*lowerIsConstantIntrinsic(IntrinsicInst
*II
) {
48 if (auto *C
= dyn_cast
<Constant
>(II
->getOperand(0)))
49 if (C
->isManifestConstant())
50 return ConstantInt::getTrue(II
->getType());
51 return ConstantInt::getFalse(II
->getType());
54 static bool replaceConditionalBranchesOnConstant(Instruction
*II
,
56 DomTreeUpdater
*DTU
) {
57 bool HasDeadBlocks
= false;
58 SmallSetVector
<Instruction
*, 8> Worklist
;
59 replaceAndRecursivelySimplify(II
, NewValue
, nullptr, nullptr, nullptr,
61 for (auto I
: Worklist
) {
62 BranchInst
*BI
= dyn_cast
<BranchInst
>(I
);
65 if (BI
->isUnconditional())
68 BasicBlock
*Target
, *Other
;
69 if (match(BI
->getOperand(0), m_Zero())) {
70 Target
= BI
->getSuccessor(1);
71 Other
= BI
->getSuccessor(0);
72 } else if (match(BI
->getOperand(0), m_One())) {
73 Target
= BI
->getSuccessor(0);
74 Other
= BI
->getSuccessor(1);
79 if (Target
&& Target
!= Other
) {
80 BasicBlock
*Source
= BI
->getParent();
81 Other
->removePredecessor(Source
);
82 BI
->eraseFromParent();
83 BranchInst::Create(Target
, Source
);
85 DTU
->applyUpdates({{DominatorTree::Delete
, Source
, Other
}});
86 if (pred_empty(Other
))
93 static bool lowerConstantIntrinsics(Function
&F
, const TargetLibraryInfo
*TLI
,
95 Optional
<DomTreeUpdater
> DTU
;
97 DTU
.emplace(DT
, DomTreeUpdater::UpdateStrategy::Lazy
);
99 bool HasDeadBlocks
= false;
100 const auto &DL
= F
.getParent()->getDataLayout();
101 SmallVector
<WeakTrackingVH
, 8> Worklist
;
103 ReversePostOrderTraversal
<Function
*> RPOT(&F
);
104 for (BasicBlock
*BB
: RPOT
) {
105 for (Instruction
&I
: *BB
) {
106 IntrinsicInst
*II
= dyn_cast
<IntrinsicInst
>(&I
);
109 switch (II
->getIntrinsicID()) {
112 case Intrinsic::is_constant
:
113 case Intrinsic::objectsize
:
114 Worklist
.push_back(WeakTrackingVH(&I
));
119 for (WeakTrackingVH
&VH
: Worklist
) {
120 // Items on the worklist can be mutated by earlier recursive replaces.
121 // This can remove the intrinsic as dead (VH == null), but also replace
122 // the intrinsic in place.
125 IntrinsicInst
*II
= dyn_cast
<IntrinsicInst
>(&*VH
);
129 switch (II
->getIntrinsicID()) {
132 case Intrinsic::is_constant
:
133 NewValue
= lowerIsConstantIntrinsic(II
);
134 IsConstantIntrinsicsHandled
++;
136 case Intrinsic::objectsize
:
137 NewValue
= lowerObjectSizeCall(II
, DL
, TLI
, true);
138 ObjectSizeIntrinsicsHandled
++;
141 HasDeadBlocks
|= replaceConditionalBranchesOnConstant(
142 II
, NewValue
, DTU
.hasValue() ? DTU
.getPointer() : nullptr);
145 removeUnreachableBlocks(F
, DTU
.hasValue() ? DTU
.getPointer() : nullptr);
146 return !Worklist
.empty();
150 LowerConstantIntrinsicsPass::run(Function
&F
, FunctionAnalysisManager
&AM
) {
151 if (lowerConstantIntrinsics(F
, AM
.getCachedResult
<TargetLibraryAnalysis
>(F
),
152 AM
.getCachedResult
<DominatorTreeAnalysis
>(F
))) {
153 PreservedAnalyses PA
;
154 PA
.preserve
<DominatorTreeAnalysis
>();
158 return PreservedAnalyses::all();
162 /// Legacy pass for lowering is.constant intrinsics out of the IR.
164 /// When this pass is run over a function it converts is.constant intrinsics
165 /// into 'true' or 'false'. This complements the normal constant folding
166 /// to 'true' as part of Instruction Simplify passes.
167 class LowerConstantIntrinsics
: public FunctionPass
{
170 LowerConstantIntrinsics() : FunctionPass(ID
) {
171 initializeLowerConstantIntrinsicsPass(*PassRegistry::getPassRegistry());
174 bool runOnFunction(Function
&F
) override
{
175 auto *TLIP
= getAnalysisIfAvailable
<TargetLibraryInfoWrapperPass
>();
176 const TargetLibraryInfo
*TLI
= TLIP
? &TLIP
->getTLI(F
) : nullptr;
177 DominatorTree
*DT
= nullptr;
178 if (auto *DTWP
= getAnalysisIfAvailable
<DominatorTreeWrapperPass
>())
179 DT
= &DTWP
->getDomTree();
180 return lowerConstantIntrinsics(F
, TLI
, DT
);
183 void getAnalysisUsage(AnalysisUsage
&AU
) const override
{
184 AU
.addPreserved
<GlobalsAAWrapperPass
>();
185 AU
.addPreserved
<DominatorTreeWrapperPass
>();
190 char LowerConstantIntrinsics::ID
= 0;
191 INITIALIZE_PASS_BEGIN(LowerConstantIntrinsics
, "lower-constant-intrinsics",
192 "Lower constant intrinsics", false, false)
193 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass
)
194 INITIALIZE_PASS_END(LowerConstantIntrinsics
, "lower-constant-intrinsics",
195 "Lower constant intrinsics", false, false)
197 FunctionPass
*llvm::createLowerConstantIntrinsicsPass() {
198 return new LowerConstantIntrinsics();