LLVM  10.0.0
LowerConstantIntrinsics.cpp
Go to the documentation of this file.
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 
16 #include "llvm/ADT/Statistic.h"
20 #include "llvm/IR/BasicBlock.h"
21 #include "llvm/IR/Constants.h"
22 #include "llvm/IR/Function.h"
23 #include "llvm/IR/Instructions.h"
24 #include "llvm/IR/IntrinsicInst.h"
25 #include "llvm/IR/Intrinsics.h"
26 #include "llvm/IR/PatternMatch.h"
27 #include "llvm/InitializePasses.h"
28 #include "llvm/Pass.h"
29 #include "llvm/Support/Debug.h"
30 #include "llvm/Transforms/Scalar.h"
32 
33 using namespace llvm;
34 using namespace llvm::PatternMatch;
35 
36 #define DEBUG_TYPE "lower-is-constant-intrinsic"
37 
38 STATISTIC(IsConstantIntrinsicsHandled,
39  "Number of 'is.constant' intrinsic calls handled");
40 STATISTIC(ObjectSizeIntrinsicsHandled,
41  "Number of 'objectsize' intrinsic calls handled");
42 
44  Value *Op = II->getOperand(0);
45 
46  return isa<Constant>(Op) ? ConstantInt::getTrue(II->getType())
48 }
49 
51  Value *NewValue) {
52  bool HasDeadBlocks = false;
54  replaceAndRecursivelySimplify(II, NewValue, nullptr, nullptr, nullptr,
55  &Worklist);
56  for (auto I : Worklist) {
58  if (!BI)
59  continue;
60  if (BI->isUnconditional())
61  continue;
62 
64  if (match(BI->getOperand(0), m_Zero())) {
65  Target = BI->getSuccessor(1);
66  Other = BI->getSuccessor(0);
67  } else if (match(BI->getOperand(0), m_One())) {
68  Target = BI->getSuccessor(0);
69  Other = BI->getSuccessor(1);
70  } else {
71  Target = nullptr;
72  Other = nullptr;
73  }
74  if (Target && Target != Other) {
75  BasicBlock *Source = BI->getParent();
76  Other->removePredecessor(Source);
77  BI->eraseFromParent();
78  BranchInst::Create(Target, Source);
79  if (pred_begin(Other) == pred_end(Other))
80  HasDeadBlocks = true;
81  }
82  }
83  return HasDeadBlocks;
84 }
85 
87  bool HasDeadBlocks = false;
88  const auto &DL = F.getParent()->getDataLayout();
90 
92  for (BasicBlock *BB : RPOT) {
93  for (Instruction &I: *BB) {
95  if (!II)
96  continue;
97  switch (II->getIntrinsicID()) {
98  default:
99  break;
100  case Intrinsic::is_constant:
101  case Intrinsic::objectsize:
102  Worklist.push_back(WeakTrackingVH(&I));
103  break;
104  }
105  }
106  }
107  for (WeakTrackingVH &VH: Worklist) {
108  // Items on the worklist can be mutated by earlier recursive replaces.
109  // This can remove the intrinsic as dead (VH == null), but also replace
110  // the intrinsic in place.
111  if (!VH)
112  continue;
113  IntrinsicInst *II = dyn_cast<IntrinsicInst>(&*VH);
114  if (!II)
115  continue;
116  Value *NewValue;
117  switch (II->getIntrinsicID()) {
118  default:
119  continue;
120  case Intrinsic::is_constant:
121  NewValue = lowerIsConstantIntrinsic(II);
122  IsConstantIntrinsicsHandled++;
123  break;
124  case Intrinsic::objectsize:
125  NewValue = lowerObjectSizeCall(II, DL, TLI, true);
126  ObjectSizeIntrinsicsHandled++;
127  break;
128  }
129  HasDeadBlocks |= replaceConditionalBranchesOnConstant(II, NewValue);
130  }
131  if (HasDeadBlocks)
133  return !Worklist.empty();
134 }
135 
139  return PreservedAnalyses::none();
140 
141  return PreservedAnalyses::all();
142 }
143 
144 namespace {
145 /// Legacy pass for lowering is.constant intrinsics out of the IR.
146 ///
147 /// When this pass is run over a function it converts is.constant intrinsics
148 /// into 'true' or 'false'. This is completements the normal constand folding
149 /// to 'true' as part of Instruction Simplify passes.
150 class LowerConstantIntrinsics : public FunctionPass {
151 public:
152  static char ID;
153  LowerConstantIntrinsics() : FunctionPass(ID) {
155  }
156 
157  bool runOnFunction(Function &F) override {
158  auto *TLIP = getAnalysisIfAvailable<TargetLibraryInfoWrapperPass>();
159  const TargetLibraryInfo *TLI = TLIP ? &TLIP->getTLI(F) : nullptr;
160  return lowerConstantIntrinsics(F, TLI);
161  }
162 };
163 } // namespace
164 
166 INITIALIZE_PASS(LowerConstantIntrinsics, "lower-constant-intrinsics",
167  "Lower constant intrinsics", false, false)
168 
170  return new LowerConstantIntrinsics();
171 }
SymbolTableList< Instruction >::iterator eraseFromParent()
This method unlinks &#39;this&#39; from the containing basic block and deletes it.
Definition: Instruction.cpp:67
static ConstantInt * getFalse(LLVMContext &Context)
Definition: Constants.cpp:667
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
is_zero m_Zero()
Match any null constant or a vector with all elements equal to 0.
Definition: PatternMatch.h:426
This class represents lattice values for constants.
Definition: AllocatorList.h:23
void removePredecessor(BasicBlock *Pred, bool KeepOneInputPHIs=false)
Notify the BasicBlock that the predecessor Pred is no longer able to reach it.
Definition: BasicBlock.cpp:308
Optional< std::vector< StOtherPiece > > Other
Definition: ELFYAML.cpp:974
BasicBlock * getSuccessor(unsigned i) const
STATISTIC(NumFunctions, "Total number of functions")
F(f)
void initializeLowerConstantIntrinsicsPass(PassRegistry &)
static Value * lowerIsConstantIntrinsic(IntrinsicInst *II)
bool match(Val *V, const Pattern &P)
Definition: PatternMatch.h:48
bool replaceAndRecursivelySimplify(Instruction *I, Value *SimpleV, const TargetLibraryInfo *TLI=nullptr, const DominatorTree *DT=nullptr, AssumptionCache *AC=nullptr, SmallSetVector< Instruction *, 8 > *UnsimplifiedUsers=nullptr)
Replace all uses of &#39;I&#39; with &#39;SimpleV&#39; and simplify the uses recursively.
const DataLayout & getDataLayout() const
Get the data layout for the module&#39;s target platform.
Definition: Module.cpp:369
Value * lowerObjectSizeCall(IntrinsicInst *ObjectSize, const DataLayout &DL, const TargetLibraryInfo *TLI, bool MustSucceed)
Try to turn a call to @llvm.objectsize into an integer value of the given Type.
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:246
Value handle that is nullable, but tries to track the Value.
Definition: ValueHandle.h:200
Value * getOperand(unsigned i) const
Definition: User.h:169
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Definition: PassManager.h:157
static bool runOnFunction(Function &F, bool PostInlining)
static bool lowerConstantIntrinsics(Function &F, const TargetLibraryInfo *TLI)
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:154
LLVM Basic Block Representation.
Definition: BasicBlock.h:57
Conditional or Unconditional Branch instruction.
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Interval::pred_iterator pred_begin(Interval *I)
pred_begin/pred_end - define methods so that Intervals may be used just like BasicBlocks can with the...
Definition: Interval.h:112
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:282
Interval::pred_iterator pred_end(Interval *I)
Definition: Interval.h:115
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:160
PreservedAnalyses run(Function &F, FunctionAnalysisManager &)
Run the pass over the function.
Intrinsic::ID getIntrinsicID() const
Return the intrinsic ID of this intrinsic.
Definition: IntrinsicInst.h:51
A SetVector that performs no allocations if smaller than a certain size.
Definition: SetVector.h:297
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:837
Provides information about what library functions are available for the current target.
FunctionPass * createLowerConstantIntrinsicsPass()
static BranchInst * Create(BasicBlock *IfTrue, Instruction *InsertBefore=nullptr)
static ConstantInt * getTrue(LLVMContext &Context)
Definition: Constants.cpp:660
Target - Wrapper for Target specific information.
static bool replaceConditionalBranchesOnConstant(Instruction *II, Value *NewValue)
#define I(x, y, z)
Definition: MD5.cpp:58
PassT::Result * getCachedResult(IRUnitT &IR) const
Get the cached result of an analysis pass for a given IR unit.
Definition: PassManager.h:796
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:332
bool removeUnreachableBlocks(Function &F, DomTreeUpdater *DTU=nullptr, MemorySSAUpdater *MSSAU=nullptr)
Remove all blocks that can not be reached from the function&#39;s entry.
Definition: Local.cpp:2231
bool isUnconditional() const
Analysis pass providing the TargetLibraryInfo.
INITIALIZE_PASS(LowerConstantIntrinsics, "lower-constant-intrinsics", "Lower constant intrinsics", false, false) FunctionPass *llvm
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:575
LLVM Value Representation.
Definition: Value.h:74
cst_pred_ty< is_one > m_One()
Match an integer 1 or a vector with all elements equal to 1.
Definition: PatternMatch.h:405
A container for analyses that lazily runs them and caches their results.
The header file for the LowerConstantIntrinsics pass as used by the new pass manager.
A wrapper class for inspecting calls to intrinsic functions.
Definition: IntrinsicInst.h:44
const BasicBlock * getParent() const
Definition: Instruction.h:66