LLVM API Documentation
00001 //===-- Local.h - Functions to perform local transformations ----*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This family of functions perform various local transformations to the 00011 // program. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #ifndef LLVM_TRANSFORMS_UTILS_LOCAL_H 00016 #define LLVM_TRANSFORMS_UTILS_LOCAL_H 00017 00018 #include "llvm/IR/DataLayout.h" 00019 #include "llvm/IR/IRBuilder.h" 00020 #include "llvm/IR/Operator.h" 00021 #include "llvm/Support/GetElementPtrTypeIterator.h" 00022 00023 namespace llvm { 00024 00025 class User; 00026 class BasicBlock; 00027 class Function; 00028 class BranchInst; 00029 class Instruction; 00030 class DbgDeclareInst; 00031 class StoreInst; 00032 class LoadInst; 00033 class Value; 00034 class Pass; 00035 class PHINode; 00036 class AllocaInst; 00037 class ConstantExpr; 00038 class DataLayout; 00039 class TargetLibraryInfo; 00040 class TargetTransformInfo; 00041 class DIBuilder; 00042 00043 template<typename T> class SmallVectorImpl; 00044 00045 //===----------------------------------------------------------------------===// 00046 // Local constant propagation. 00047 // 00048 00049 /// ConstantFoldTerminator - If a terminator instruction is predicated on a 00050 /// constant value, convert it into an unconditional branch to the constant 00051 /// destination. This is a nontrivial operation because the successors of this 00052 /// basic block must have their PHI nodes updated. 00053 /// Also calls RecursivelyDeleteTriviallyDeadInstructions() on any branch/switch 00054 /// conditions and indirectbr addresses this might make dead if 00055 /// DeleteDeadConditions is true. 00056 bool ConstantFoldTerminator(BasicBlock *BB, bool DeleteDeadConditions = false, 00057 const TargetLibraryInfo *TLI = 0); 00058 00059 //===----------------------------------------------------------------------===// 00060 // Local dead code elimination. 00061 // 00062 00063 /// isInstructionTriviallyDead - Return true if the result produced by the 00064 /// instruction is not used, and the instruction has no side effects. 00065 /// 00066 bool isInstructionTriviallyDead(Instruction *I, const TargetLibraryInfo *TLI=0); 00067 00068 /// RecursivelyDeleteTriviallyDeadInstructions - If the specified value is a 00069 /// trivially dead instruction, delete it. If that makes any of its operands 00070 /// trivially dead, delete them too, recursively. Return true if any 00071 /// instructions were deleted. 00072 bool RecursivelyDeleteTriviallyDeadInstructions(Value *V, 00073 const TargetLibraryInfo *TLI=0); 00074 00075 /// RecursivelyDeleteDeadPHINode - If the specified value is an effectively 00076 /// dead PHI node, due to being a def-use chain of single-use nodes that 00077 /// either forms a cycle or is terminated by a trivially dead instruction, 00078 /// delete it. If that makes any of its operands trivially dead, delete them 00079 /// too, recursively. Return true if a change was made. 00080 bool RecursivelyDeleteDeadPHINode(PHINode *PN, const TargetLibraryInfo *TLI=0); 00081 00082 00083 /// SimplifyInstructionsInBlock - Scan the specified basic block and try to 00084 /// simplify any instructions in it and recursively delete dead instructions. 00085 /// 00086 /// This returns true if it changed the code, note that it can delete 00087 /// instructions in other blocks as well in this block. 00088 bool SimplifyInstructionsInBlock(BasicBlock *BB, const DataLayout *TD = 0, 00089 const TargetLibraryInfo *TLI = 0); 00090 00091 //===----------------------------------------------------------------------===// 00092 // Control Flow Graph Restructuring. 00093 // 00094 00095 /// RemovePredecessorAndSimplify - Like BasicBlock::removePredecessor, this 00096 /// method is called when we're about to delete Pred as a predecessor of BB. If 00097 /// BB contains any PHI nodes, this drops the entries in the PHI nodes for Pred. 00098 /// 00099 /// Unlike the removePredecessor method, this attempts to simplify uses of PHI 00100 /// nodes that collapse into identity values. For example, if we have: 00101 /// x = phi(1, 0, 0, 0) 00102 /// y = and x, z 00103 /// 00104 /// .. and delete the predecessor corresponding to the '1', this will attempt to 00105 /// recursively fold the 'and' to 0. 00106 void RemovePredecessorAndSimplify(BasicBlock *BB, BasicBlock *Pred, 00107 DataLayout *TD = 0); 00108 00109 00110 /// MergeBasicBlockIntoOnlyPred - BB is a block with one predecessor and its 00111 /// predecessor is known to have one successor (BB!). Eliminate the edge 00112 /// between them, moving the instructions in the predecessor into BB. This 00113 /// deletes the predecessor block. 00114 /// 00115 void MergeBasicBlockIntoOnlyPred(BasicBlock *BB, Pass *P = 0); 00116 00117 00118 /// TryToSimplifyUncondBranchFromEmptyBlock - BB is known to contain an 00119 /// unconditional branch, and contains no instructions other than PHI nodes, 00120 /// potential debug intrinsics and the branch. If possible, eliminate BB by 00121 /// rewriting all the predecessors to branch to the successor block and return 00122 /// true. If we can't transform, return false. 00123 bool TryToSimplifyUncondBranchFromEmptyBlock(BasicBlock *BB); 00124 00125 /// EliminateDuplicatePHINodes - Check for and eliminate duplicate PHI 00126 /// nodes in this block. This doesn't try to be clever about PHI nodes 00127 /// which differ only in the order of the incoming values, but instcombine 00128 /// orders them so it usually won't matter. 00129 /// 00130 bool EliminateDuplicatePHINodes(BasicBlock *BB); 00131 00132 /// SimplifyCFG - This function is used to do simplification of a CFG. For 00133 /// example, it adjusts branches to branches to eliminate the extra hop, it 00134 /// eliminates unreachable basic blocks, and does other "peephole" optimization 00135 /// of the CFG. It returns true if a modification was made, possibly deleting 00136 /// the basic block that was pointed to. 00137 /// 00138 bool SimplifyCFG(BasicBlock *BB, const TargetTransformInfo &TTI, 00139 const DataLayout *TD = 0); 00140 00141 /// FoldBranchToCommonDest - If this basic block is ONLY a setcc and a branch, 00142 /// and if a predecessor branches to us and one of our successors, fold the 00143 /// setcc into the predecessor and use logical operations to pick the right 00144 /// destination. 00145 bool FoldBranchToCommonDest(BranchInst *BI); 00146 00147 /// DemoteRegToStack - This function takes a virtual register computed by an 00148 /// Instruction and replaces it with a slot in the stack frame, allocated via 00149 /// alloca. This allows the CFG to be changed around without fear of 00150 /// invalidating the SSA information for the value. It returns the pointer to 00151 /// the alloca inserted to create a stack slot for X. 00152 /// 00153 AllocaInst *DemoteRegToStack(Instruction &X, 00154 bool VolatileLoads = false, 00155 Instruction *AllocaPoint = 0); 00156 00157 /// DemotePHIToStack - This function takes a virtual register computed by a phi 00158 /// node and replaces it with a slot in the stack frame, allocated via alloca. 00159 /// The phi node is deleted and it returns the pointer to the alloca inserted. 00160 AllocaInst *DemotePHIToStack(PHINode *P, Instruction *AllocaPoint = 0); 00161 00162 /// getOrEnforceKnownAlignment - If the specified pointer has an alignment that 00163 /// we can determine, return it, otherwise return 0. If PrefAlign is specified, 00164 /// and it is more than the alignment of the ultimate object, see if we can 00165 /// increase the alignment of the ultimate object, making this check succeed. 00166 unsigned getOrEnforceKnownAlignment(Value *V, unsigned PrefAlign, 00167 const DataLayout *TD = 0); 00168 00169 /// getKnownAlignment - Try to infer an alignment for the specified pointer. 00170 static inline unsigned getKnownAlignment(Value *V, const DataLayout *TD = 0) { 00171 return getOrEnforceKnownAlignment(V, 0, TD); 00172 } 00173 00174 /// EmitGEPOffset - Given a getelementptr instruction/constantexpr, emit the 00175 /// code necessary to compute the offset from the base pointer (without adding 00176 /// in the base pointer). Return the result as a signed integer of intptr size. 00177 /// When NoAssumptions is true, no assumptions about index computation not 00178 /// overflowing is made. 00179 template<typename IRBuilderTy> 00180 Value *EmitGEPOffset(IRBuilderTy *Builder, const DataLayout &TD, User *GEP, 00181 bool NoAssumptions = false) { 00182 gep_type_iterator GTI = gep_type_begin(GEP); 00183 Type *IntPtrTy = TD.getIntPtrType(GEP->getContext()); 00184 Value *Result = Constant::getNullValue(IntPtrTy); 00185 00186 // If the GEP is inbounds, we know that none of the addressing operations will 00187 // overflow in an unsigned sense. 00188 bool isInBounds = cast<GEPOperator>(GEP)->isInBounds() && !NoAssumptions; 00189 00190 // Build a mask for high order bits. 00191 unsigned IntPtrWidth = TD.getPointerSizeInBits(); 00192 uint64_t PtrSizeMask = ~0ULL >> (64-IntPtrWidth); 00193 00194 for (User::op_iterator i = GEP->op_begin() + 1, e = GEP->op_end(); i != e; 00195 ++i, ++GTI) { 00196 Value *Op = *i; 00197 uint64_t Size = TD.getTypeAllocSize(GTI.getIndexedType()) & PtrSizeMask; 00198 if (ConstantInt *OpC = dyn_cast<ConstantInt>(Op)) { 00199 if (OpC->isZero()) continue; 00200 00201 // Handle a struct index, which adds its field offset to the pointer. 00202 if (StructType *STy = dyn_cast<StructType>(*GTI)) { 00203 Size = TD.getStructLayout(STy)->getElementOffset(OpC->getZExtValue()); 00204 00205 if (Size) 00206 Result = Builder->CreateAdd(Result, ConstantInt::get(IntPtrTy, Size), 00207 GEP->getName()+".offs"); 00208 continue; 00209 } 00210 00211 Constant *Scale = ConstantInt::get(IntPtrTy, Size); 00212 Constant *OC = ConstantExpr::getIntegerCast(OpC, IntPtrTy, true /*SExt*/); 00213 Scale = ConstantExpr::getMul(OC, Scale, isInBounds/*NUW*/); 00214 // Emit an add instruction. 00215 Result = Builder->CreateAdd(Result, Scale, GEP->getName()+".offs"); 00216 continue; 00217 } 00218 // Convert to correct type. 00219 if (Op->getType() != IntPtrTy) 00220 Op = Builder->CreateIntCast(Op, IntPtrTy, true, Op->getName()+".c"); 00221 if (Size != 1) { 00222 // We'll let instcombine(mul) convert this to a shl if possible. 00223 Op = Builder->CreateMul(Op, ConstantInt::get(IntPtrTy, Size), 00224 GEP->getName()+".idx", isInBounds /*NUW*/); 00225 } 00226 00227 // Emit an add instruction. 00228 Result = Builder->CreateAdd(Op, Result, GEP->getName()+".offs"); 00229 } 00230 return Result; 00231 } 00232 00233 ///===---------------------------------------------------------------------===// 00234 /// Dbg Intrinsic utilities 00235 /// 00236 00237 /// Inserts a llvm.dbg.value intrinsic before a store to an alloca'd value 00238 /// that has an associated llvm.dbg.decl intrinsic. 00239 bool ConvertDebugDeclareToDebugValue(DbgDeclareInst *DDI, 00240 StoreInst *SI, DIBuilder &Builder); 00241 00242 /// Inserts a llvm.dbg.value intrinsic before a load of an alloca'd value 00243 /// that has an associated llvm.dbg.decl intrinsic. 00244 bool ConvertDebugDeclareToDebugValue(DbgDeclareInst *DDI, 00245 LoadInst *LI, DIBuilder &Builder); 00246 00247 /// LowerDbgDeclare - Lowers llvm.dbg.declare intrinsics into appropriate set 00248 /// of llvm.dbg.value intrinsics. 00249 bool LowerDbgDeclare(Function &F); 00250 00251 /// FindAllocaDbgDeclare - Finds the llvm.dbg.declare intrinsic corresponding to 00252 /// an alloca, if any. 00253 DbgDeclareInst *FindAllocaDbgDeclare(Value *V); 00254 00255 /// replaceDbgDeclareForAlloca - Replaces llvm.dbg.declare instruction when 00256 /// alloca is replaced with a new value. 00257 bool replaceDbgDeclareForAlloca(AllocaInst *AI, Value *NewAllocaAddress, 00258 DIBuilder &Builder); 00259 00260 /// \brief Remove all blocks that can not be reached from the function's entry. 00261 /// 00262 /// Returns true if any basic block was removed. 00263 bool removeUnreachableBlocks(Function &F); 00264 00265 } // End llvm namespace 00266 00267 #endif