LLVM  3.7.0
Local.h
Go to the documentation of this file.
1 //===-- Local.h - Functions to perform local transformations ----*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This family of functions perform various local transformations to the
11 // program.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_TRANSFORMS_UTILS_LOCAL_H
16 #define LLVM_TRANSFORMS_UTILS_LOCAL_H
17 
18 #include "llvm/IR/DataLayout.h"
19 #include "llvm/IR/Dominators.h"
21 #include "llvm/IR/IRBuilder.h"
22 #include "llvm/IR/Operator.h"
23 
24 namespace llvm {
25 
26 class User;
27 class BasicBlock;
28 class Function;
29 class BranchInst;
30 class Instruction;
31 class DbgDeclareInst;
32 class StoreInst;
33 class LoadInst;
34 class Value;
35 class PHINode;
36 class AllocaInst;
37 class AssumptionCache;
38 class ConstantExpr;
39 class DataLayout;
40 class TargetLibraryInfo;
41 class TargetTransformInfo;
42 class DIBuilder;
43 class AliasAnalysis;
44 class DominatorTree;
45 
46 template<typename T> class SmallVectorImpl;
47 
48 //===----------------------------------------------------------------------===//
49 // Local constant propagation.
50 //
51 
52 /// ConstantFoldTerminator - If a terminator instruction is predicated on a
53 /// constant value, convert it into an unconditional branch to the constant
54 /// destination. This is a nontrivial operation because the successors of this
55 /// basic block must have their PHI nodes updated.
56 /// Also calls RecursivelyDeleteTriviallyDeadInstructions() on any branch/switch
57 /// conditions and indirectbr addresses this might make dead if
58 /// DeleteDeadConditions is true.
59 bool ConstantFoldTerminator(BasicBlock *BB, bool DeleteDeadConditions = false,
60  const TargetLibraryInfo *TLI = nullptr);
61 
62 //===----------------------------------------------------------------------===//
63 // Local dead code elimination.
64 //
65 
66 /// isInstructionTriviallyDead - Return true if the result produced by the
67 /// instruction is not used, and the instruction has no side effects.
68 ///
69 bool isInstructionTriviallyDead(Instruction *I,
70  const TargetLibraryInfo *TLI = nullptr);
71 
72 /// RecursivelyDeleteTriviallyDeadInstructions - If the specified value is a
73 /// trivially dead instruction, delete it. If that makes any of its operands
74 /// trivially dead, delete them too, recursively. Return true if any
75 /// instructions were deleted.
77  const TargetLibraryInfo *TLI = nullptr);
78 
79 /// RecursivelyDeleteDeadPHINode - If the specified value is an effectively
80 /// dead PHI node, due to being a def-use chain of single-use nodes that
81 /// either forms a cycle or is terminated by a trivially dead instruction,
82 /// delete it. If that makes any of its operands trivially dead, delete them
83 /// too, recursively. Return true if a change was made.
84 bool RecursivelyDeleteDeadPHINode(PHINode *PN,
85  const TargetLibraryInfo *TLI = nullptr);
86 
87 /// SimplifyInstructionsInBlock - Scan the specified basic block and try to
88 /// simplify any instructions in it and recursively delete dead instructions.
89 ///
90 /// This returns true if it changed the code, note that it can delete
91 /// instructions in other blocks as well in this block.
93  const TargetLibraryInfo *TLI = nullptr);
94 
95 //===----------------------------------------------------------------------===//
96 // Control Flow Graph Restructuring.
97 //
98 
99 /// RemovePredecessorAndSimplify - Like BasicBlock::removePredecessor, this
100 /// method is called when we're about to delete Pred as a predecessor of BB. If
101 /// BB contains any PHI nodes, this drops the entries in the PHI nodes for Pred.
102 ///
103 /// Unlike the removePredecessor method, this attempts to simplify uses of PHI
104 /// nodes that collapse into identity values. For example, if we have:
105 /// x = phi(1, 0, 0, 0)
106 /// y = and x, z
107 ///
108 /// .. and delete the predecessor corresponding to the '1', this will attempt to
109 /// recursively fold the 'and' to 0.
111 
112 /// MergeBasicBlockIntoOnlyPred - BB is a block with one predecessor and its
113 /// predecessor is known to have one successor (BB!). Eliminate the edge
114 /// between them, moving the instructions in the predecessor into BB. This
115 /// deletes the predecessor block.
116 ///
117 void MergeBasicBlockIntoOnlyPred(BasicBlock *BB, DominatorTree *DT = nullptr);
118 
119 /// TryToSimplifyUncondBranchFromEmptyBlock - BB is known to contain an
120 /// unconditional branch, and contains no instructions other than PHI nodes,
121 /// potential debug intrinsics and the branch. If possible, eliminate BB by
122 /// rewriting all the predecessors to branch to the successor block and return
123 /// true. If we can't transform, return false.
125 
126 /// EliminateDuplicatePHINodes - Check for and eliminate duplicate PHI
127 /// nodes in this block. This doesn't try to be clever about PHI nodes
128 /// which differ only in the order of the incoming values, but instcombine
129 /// orders them so it usually won't matter.
130 ///
132 
133 /// SimplifyCFG - This function is used to do simplification of a CFG. For
134 /// example, it adjusts branches to branches to eliminate the extra hop, it
135 /// eliminates unreachable basic blocks, and does other "peephole" optimization
136 /// of the CFG. It returns true if a modification was made, possibly deleting
137 /// the basic block that was pointed to.
138 ///
139 bool SimplifyCFG(BasicBlock *BB, const TargetTransformInfo &TTI,
140  unsigned BonusInstThreshold, AssumptionCache *AC = nullptr);
141 
142 /// FlatternCFG - This function is used to flatten a CFG. For
143 /// example, it uses parallel-and and parallel-or mode to collapse
144 // if-conditions and merge if-regions with identical statements.
145 ///
146 bool FlattenCFG(BasicBlock *BB, AliasAnalysis *AA = nullptr);
147 
148 /// FoldBranchToCommonDest - If this basic block is ONLY a setcc and a branch,
149 /// and if a predecessor branches to us and one of our successors, fold the
150 /// setcc into the predecessor and use logical operations to pick the right
151 /// destination.
152 bool FoldBranchToCommonDest(BranchInst *BI, unsigned BonusInstThreshold = 1);
153 
154 /// DemoteRegToStack - This function takes a virtual register computed by an
155 /// Instruction and replaces it with a slot in the stack frame, allocated via
156 /// alloca. This allows the CFG to be changed around without fear of
157 /// invalidating the SSA information for the value. It returns the pointer to
158 /// the alloca inserted to create a stack slot for X.
159 ///
160 AllocaInst *DemoteRegToStack(Instruction &X,
161  bool VolatileLoads = false,
162  Instruction *AllocaPoint = nullptr);
163 
164 /// DemotePHIToStack - This function takes a virtual register computed by a phi
165 /// node and replaces it with a slot in the stack frame, allocated via alloca.
166 /// The phi node is deleted and it returns the pointer to the alloca inserted.
167 AllocaInst *DemotePHIToStack(PHINode *P, Instruction *AllocaPoint = nullptr);
168 
169 /// getOrEnforceKnownAlignment - If the specified pointer has an alignment that
170 /// we can determine, return it, otherwise return 0. If PrefAlign is specified,
171 /// and it is more than the alignment of the ultimate object, see if we can
172 /// increase the alignment of the ultimate object, making this check succeed.
173 unsigned getOrEnforceKnownAlignment(Value *V, unsigned PrefAlign,
174  const DataLayout &DL,
175  const Instruction *CxtI = nullptr,
176  AssumptionCache *AC = nullptr,
177  const DominatorTree *DT = nullptr);
178 
179 /// getKnownAlignment - Try to infer an alignment for the specified pointer.
180 static inline unsigned getKnownAlignment(Value *V, const DataLayout &DL,
181  const Instruction *CxtI = nullptr,
182  AssumptionCache *AC = nullptr,
183  const DominatorTree *DT = nullptr) {
184  return getOrEnforceKnownAlignment(V, 0, DL, CxtI, AC, DT);
185 }
186 
187 /// EmitGEPOffset - Given a getelementptr instruction/constantexpr, emit the
188 /// code necessary to compute the offset from the base pointer (without adding
189 /// in the base pointer). Return the result as a signed integer of intptr size.
190 /// When NoAssumptions is true, no assumptions about index computation not
191 /// overflowing is made.
192 template <typename IRBuilderTy>
193 Value *EmitGEPOffset(IRBuilderTy *Builder, const DataLayout &DL, User *GEP,
194  bool NoAssumptions = false) {
195  GEPOperator *GEPOp = cast<GEPOperator>(GEP);
196  Type *IntPtrTy = DL.getIntPtrType(GEP->getType());
197  Value *Result = Constant::getNullValue(IntPtrTy);
198 
199  // If the GEP is inbounds, we know that none of the addressing operations will
200  // overflow in an unsigned sense.
201  bool isInBounds = GEPOp->isInBounds() && !NoAssumptions;
202 
203  // Build a mask for high order bits.
204  unsigned IntPtrWidth = IntPtrTy->getScalarType()->getIntegerBitWidth();
205  uint64_t PtrSizeMask = ~0ULL >> (64 - IntPtrWidth);
206 
208  for (User::op_iterator i = GEP->op_begin() + 1, e = GEP->op_end(); i != e;
209  ++i, ++GTI) {
210  Value *Op = *i;
211  uint64_t Size = DL.getTypeAllocSize(GTI.getIndexedType()) & PtrSizeMask;
212  if (Constant *OpC = dyn_cast<Constant>(Op)) {
213  if (OpC->isZeroValue())
214  continue;
215 
216  // Handle a struct index, which adds its field offset to the pointer.
217  if (StructType *STy = dyn_cast<StructType>(*GTI)) {
218  if (OpC->getType()->isVectorTy())
219  OpC = OpC->getSplatValue();
220 
221  uint64_t OpValue = cast<ConstantInt>(OpC)->getZExtValue();
222  Size = DL.getStructLayout(STy)->getElementOffset(OpValue);
223 
224  if (Size)
225  Result = Builder->CreateAdd(Result, ConstantInt::get(IntPtrTy, Size),
226  GEP->getName()+".offs");
227  continue;
228  }
229 
230  Constant *Scale = ConstantInt::get(IntPtrTy, Size);
231  Constant *OC = ConstantExpr::getIntegerCast(OpC, IntPtrTy, true /*SExt*/);
232  Scale = ConstantExpr::getMul(OC, Scale, isInBounds/*NUW*/);
233  // Emit an add instruction.
234  Result = Builder->CreateAdd(Result, Scale, GEP->getName()+".offs");
235  continue;
236  }
237  // Convert to correct type.
238  if (Op->getType() != IntPtrTy)
239  Op = Builder->CreateIntCast(Op, IntPtrTy, true, Op->getName()+".c");
240  if (Size != 1) {
241  // We'll let instcombine(mul) convert this to a shl if possible.
242  Op = Builder->CreateMul(Op, ConstantInt::get(IntPtrTy, Size),
243  GEP->getName()+".idx", isInBounds /*NUW*/);
244  }
245 
246  // Emit an add instruction.
247  Result = Builder->CreateAdd(Op, Result, GEP->getName()+".offs");
248  }
249  return Result;
250 }
251 
252 ///===---------------------------------------------------------------------===//
253 /// Dbg Intrinsic utilities
254 ///
255 
256 /// Inserts a llvm.dbg.value intrinsic before a store to an alloca'd value
257 /// that has an associated llvm.dbg.decl intrinsic.
258 bool ConvertDebugDeclareToDebugValue(DbgDeclareInst *DDI,
259  StoreInst *SI, DIBuilder &Builder);
260 
261 /// Inserts a llvm.dbg.value intrinsic before a load of an alloca'd value
262 /// that has an associated llvm.dbg.decl intrinsic.
263 bool ConvertDebugDeclareToDebugValue(DbgDeclareInst *DDI,
264  LoadInst *LI, DIBuilder &Builder);
265 
266 /// LowerDbgDeclare - Lowers llvm.dbg.declare intrinsics into appropriate set
267 /// of llvm.dbg.value intrinsics.
268 bool LowerDbgDeclare(Function &F);
269 
270 /// FindAllocaDbgDeclare - Finds the llvm.dbg.declare intrinsic corresponding to
271 /// an alloca, if any.
272 DbgDeclareInst *FindAllocaDbgDeclare(Value *V);
273 
274 /// \brief Replaces llvm.dbg.declare instruction when an alloca is replaced with
275 /// a new value. If Deref is true, tan additional DW_OP_deref is prepended to
276 /// the expression.
277 bool replaceDbgDeclareForAlloca(AllocaInst *AI, Value *NewAllocaAddress,
278  DIBuilder &Builder, bool Deref);
279 
280 /// \brief Remove all blocks that can not be reached from the function's entry.
281 ///
282 /// Returns true if any basic block was removed.
283 bool removeUnreachableBlocks(Function &F);
284 
285 /// \brief Combine the metadata of two instructions so that K can replace J
286 ///
287 /// Metadata not listed as known via KnownIDs is removed
288 void combineMetadata(Instruction *K, const Instruction *J, ArrayRef<unsigned> KnownIDs);
289 
290 /// \brief Replace each use of 'From' with 'To' if that use is dominated by
291 /// the given edge. Returns the number of replacements made.
292 unsigned replaceDominatedUsesWith(Value *From, Value *To, DominatorTree &DT,
293  const BasicBlockEdge &Edge);
294 } // End llvm namespace
295 
296 #endif
Value * EmitGEPOffset(IRBuilderTy *Builder, const DataLayout &DL, User *GEP, bool NoAssumptions=false)
EmitGEPOffset - Given a getelementptr instruction/constantexpr, emit the code necessary to compute th...
Definition: Local.h:193
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:104
AllocaInst * DemoteRegToStack(Instruction &X, bool VolatileLoads=false, Instruction *AllocaPoint=nullptr)
DemoteRegToStack - This function takes a virtual register computed by an Instruction and replaces it ...
unsigned getOrEnforceKnownAlignment(Value *V, unsigned PrefAlign, const DataLayout &DL, const Instruction *CxtI=nullptr, AssumptionCache *AC=nullptr, const DominatorTree *DT=nullptr)
getOrEnforceKnownAlignment - If the specified pointer has an alignment that we can determine...
Definition: Local.cpp:927
Various leaf nodes.
Definition: ISDOpcodes.h:60
DbgDeclareInst * FindAllocaDbgDeclare(Value *V)
FindAllocaDbgDeclare - Finds the llvm.dbg.declare intrinsic corresponding to an alloca, if any.
Definition: Local.cpp:1070
void MergeBasicBlockIntoOnlyPred(BasicBlock *BB, DominatorTree *DT=nullptr)
MergeBasicBlockIntoOnlyPred - BB is a block with one predecessor and its predecessor is known to have...
Definition: Local.cpp:500
A cache of .assume calls within a function.
bool SimplifyInstructionsInBlock(BasicBlock *BB, const TargetLibraryInfo *TLI=nullptr)
SimplifyInstructionsInBlock - Scan the specified basic block and try to simplify any instructions in ...
Definition: Local.cpp:422
F(f)
Hexagon Common GEP
void RemovePredecessorAndSimplify(BasicBlock *BB, BasicBlock *Pred)
RemovePredecessorAndSimplify - Like BasicBlock::removePredecessor, this method is called when we're a...
Definition: Local.cpp:469
unsigned replaceDominatedUsesWith(Value *From, Value *To, DominatorTree &DT, const BasicBlockEdge &Edge)
Replace each use of 'From' with 'To' if that use is dominated by the given edge.
Definition: Local.cpp:1328
op_iterator op_begin()
Definition: User.h:183
static Constant * getNullValue(Type *Ty)
Definition: Constants.cpp:178
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:188
static Constant * getIntegerCast(Constant *C, Type *Ty, bool isSigned)
Create a ZExt, Bitcast or Trunc for integer -> integer casts.
Definition: Constants.cpp:1674
const StructLayout * getStructLayout(StructType *Ty) const
Returns a StructLayout object, indicating the alignment of the struct, its size, and the offsets of i...
Definition: DataLayout.cpp:551
StructType - Class to represent struct types.
Definition: DerivedTypes.h:191
A Use represents the edge between a Value definition and its users.
Definition: Use.h:69
bool ConstantFoldTerminator(BasicBlock *BB, bool DeleteDeadConditions=false, const TargetLibraryInfo *TLI=nullptr)
ConstantFoldTerminator - If a terminator instruction is predicated on a constant value, convert it into an unconditional branch to the constant destination.
Definition: Local.cpp:64
bool EliminateDuplicatePHINodes(BasicBlock *BB)
EliminateDuplicatePHINodes - Check for and eliminate duplicate PHI nodes in this block.
Definition: Local.cpp:832
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
Definition: Dominators.h:67
static unsigned getKnownAlignment(Value *V, const DataLayout &DL, const Instruction *CxtI=nullptr, AssumptionCache *AC=nullptr, const DominatorTree *DT=nullptr)
getKnownAlignment - Try to infer an alignment for the specified pointer.
Definition: Local.h:180
bool FlattenCFG(BasicBlock *BB, AliasAnalysis *AA=nullptr)
FlatternCFG - This function is used to flatten a CFG.
Definition: FlattenCFG.cpp:487
uint64_t getElementOffset(unsigned Idx) const
Definition: DataLayout.h:491
#define P(N)
bool FoldBranchToCommonDest(BranchInst *BI, unsigned BonusInstThreshold=1)
FoldBranchToCommonDest - If this basic block is ONLY a setcc and a branch, and if a predecessor branc...
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:45
bool TryToSimplifyUncondBranchFromEmptyBlock(BasicBlock *BB)
TryToSimplifyUncondBranchFromEmptyBlock - BB is known to contain an unconditional branch...
Definition: Local.cpp:750
This is an important base class in LLVM.
Definition: Constant.h:41
op_iterator op_end()
Definition: User.h:185
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang","erlang-compatible garbage collector")
bool SimplifyCFG(BasicBlock *BB, const TargetTransformInfo &TTI, unsigned BonusInstThreshold, AssumptionCache *AC=nullptr)
SimplifyCFG - This function is used to do simplification of a CFG.
bool RecursivelyDeleteTriviallyDeadInstructions(Value *V, const TargetLibraryInfo *TLI=nullptr)
RecursivelyDeleteTriviallyDeadInstructions - If the specified value is a trivially dead instruction...
Definition: Local.cpp:340
bool LowerDbgDeclare(Function &F)
LowerDbgDeclare - Lowers llvm.dbg.declare intrinsics into appropriate set of llvm.dbg.value intrinsics.
Definition: Local.cpp:1028
IntegerType * getIntPtrType(LLVMContext &C, unsigned AddressSpace=0) const
Returns an integer type with size at least as big as that of a pointer in the given address space...
Definition: DataLayout.cpp:694
uint64_t getTypeAllocSize(Type *Ty) const
Returns the offset in bytes between successive objects of the specified type, including alignment pad...
Definition: DataLayout.h:388
bool ConvertDebugDeclareToDebugValue(DbgDeclareInst *DDI, StoreInst *SI, DIBuilder &Builder)
===---------------------------------------------------------------——===// Dbg Intrinsic utilities ...
Definition: Local.cpp:979
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:222
bool RecursivelyDeleteDeadPHINode(PHINode *PN, const TargetLibraryInfo *TLI=nullptr)
RecursivelyDeleteDeadPHINode - If the specified value is an effectively dead PHI node, due to being a def-use chain of single-use nodes that either forms a cycle or is terminated by a trivially dead instruction, delete it.
Definition: Local.cpp:397
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
If Ty is a vector type, return a Constant with a splat of the given value.
Definition: Constants.cpp:582
bool replaceDbgDeclareForAlloca(AllocaInst *AI, Value *NewAllocaAddress, DIBuilder &Builder, bool Deref)
Replaces llvm.dbg.declare instruction when an alloca is replaced with a new value.
Definition: Local.cpp:1080
could "use" a pointer
bool isInBounds() const
Test whether this is an inbounds GEP, as defined by LangRef.html.
Definition: Operator.h:379
#define I(x, y, z)
Definition: MD5.cpp:54
AllocaInst * DemotePHIToStack(PHINode *P, Instruction *AllocaPoint=nullptr)
DemotePHIToStack - This function takes a virtual register computed by a phi node and replaces it with...
bool isInstructionTriviallyDead(Instruction *I, const TargetLibraryInfo *TLI=nullptr)
isInstructionTriviallyDead - Return true if the result produced by the instruction is not used...
Definition: Local.cpp:282
LLVM Value Representation.
Definition: Value.h:69
bool removeUnreachableBlocks(Function &F)
Remove all blocks that can not be reached from the function's entry.
Definition: Local.cpp:1254
static Constant * getMul(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
Definition: Constants.cpp:2280
void combineMetadata(Instruction *K, const Instruction *J, ArrayRef< unsigned > KnownIDs)
Combine the metadata of two instructions so that K can replace J.
Definition: Local.cpp:1286
gep_type_iterator gep_type_begin(const User *GEP)