Local.h revision 5649ba70fb39f2fda4791d255ae8bb373071874f
148486893f46d2e12e926682a3ecb908716bc66c4Chris Lattner//===-- Local.h - Functions to perform local transformations ----*- C++ -*-===// 234695381d626485a560594f162701088079589dfMisha Brukman// 36fbcc26f1460eaee4e0eb8b426fc1ff0c7af11beJohn Criswell// The LLVM Compiler Infrastructure 46fbcc26f1460eaee4e0eb8b426fc1ff0c7af11beJohn Criswell// 57ed47a13356daed2a34cd2209a31f92552e3bdd8Chris Lattner// This file is distributed under the University of Illinois Open Source 67ed47a13356daed2a34cd2209a31f92552e3bdd8Chris Lattner// License. See LICENSE.TXT for details. 734695381d626485a560594f162701088079589dfMisha Brukman// 86fbcc26f1460eaee4e0eb8b426fc1ff0c7af11beJohn Criswell//===----------------------------------------------------------------------===// 9148a0bfcea5d3267eac02613cfcc8b5fe8894f2cChris Lattner// 10148a0bfcea5d3267eac02613cfcc8b5fe8894f2cChris Lattner// This family of functions perform various local transformations to the 11148a0bfcea5d3267eac02613cfcc8b5fe8894f2cChris Lattner// program. 12148a0bfcea5d3267eac02613cfcc8b5fe8894f2cChris Lattner// 13148a0bfcea5d3267eac02613cfcc8b5fe8894f2cChris Lattner//===----------------------------------------------------------------------===// 14148a0bfcea5d3267eac02613cfcc8b5fe8894f2cChris Lattner 15148a0bfcea5d3267eac02613cfcc8b5fe8894f2cChris Lattner#ifndef LLVM_TRANSFORMS_UTILS_LOCAL_H 16148a0bfcea5d3267eac02613cfcc8b5fe8894f2cChris Lattner#define LLVM_TRANSFORMS_UTILS_LOCAL_H 17148a0bfcea5d3267eac02613cfcc8b5fe8894f2cChris Lattner 18d0fde30ce850b78371fd1386338350591f9ff494Brian Gaekenamespace llvm { 19d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke 20c79e1182470ed12f1f3d0d35c1725366519a9af7Devang Patelclass User; 21d68a07650cdb2e18f18f362ba533459aa10e01b6Dan Gohmanclass BasicBlock; 22813c9a0f19c0d27085a3ea81eb44033747007741Devang Patelclass Function; 234b35f83b91a1a313f0730c600e5178aaf7df98d6Dan Gohmanclass BranchInst; 24d68a07650cdb2e18f18f362ba533459aa10e01b6Dan Gohmanclass Instruction; 255ee20680c7ebc765950983633e19fafab5235245Devang Patelclass DbgDeclareInst; 265ee20680c7ebc765950983633e19fafab5235245Devang Patelclass StoreInst; 2736fae67831517f132255118b45b21a8cf199a012Devang Patelclass LoadInst; 28d68a07650cdb2e18f18f362ba533459aa10e01b6Dan Gohmanclass Value; 293e57cb98bde04a65623493738d51f2a41e601748Chris Lattnerclass Pass; 30abbc2dd77908f146f73f4cd1abfdfe47faacf43dChris Lattnerclass PHINode; 31cbf99ee973b6c6e6cd9537dae12031382783bc61Chris Lattnerclass AllocaInst; 32c5406b55d58b7378822d5f6316bf253ad0eaa82dChris Lattnerclass ConstantExpr; 3373259caa44b03206d1e39e77a7aa375250b1a193Chris Lattnerclass TargetData; 345ee20680c7ebc765950983633e19fafab5235245Devang Patelclass DIBuilder; 35d68a07650cdb2e18f18f362ba533459aa10e01b6Dan Gohman 36d68a07650cdb2e18f18f362ba533459aa10e01b6Dan Gohmantemplate<typename T> class SmallVectorImpl; 374f02c74a8ec8f4ca83b823146cb9c987ed79b3f6Chris Lattner 38148a0bfcea5d3267eac02613cfcc8b5fe8894f2cChris Lattner//===----------------------------------------------------------------------===// 393481f24c06b3c9de48bdd99c37547471ca8e761eChris Lattner// Local constant propagation. 40148a0bfcea5d3267eac02613cfcc8b5fe8894f2cChris Lattner// 41148a0bfcea5d3267eac02613cfcc8b5fe8894f2cChris Lattner 423e57cb98bde04a65623493738d51f2a41e601748Chris Lattner/// ConstantFoldTerminator - If a terminator instruction is predicated on a 433e57cb98bde04a65623493738d51f2a41e601748Chris Lattner/// constant value, convert it into an unconditional branch to the constant 443e57cb98bde04a65623493738d51f2a41e601748Chris Lattner/// destination. This is a nontrivial operation because the successors of this 453e57cb98bde04a65623493738d51f2a41e601748Chris Lattner/// basic block must have their PHI nodes updated. 465649ba70fb39f2fda4791d255ae8bb373071874fFrits van Bommel/// Also calls RecursivelyDeleteTriviallyDeadInstructions() on any branch/switch 475649ba70fb39f2fda4791d255ae8bb373071874fFrits van Bommel/// conditions and indirectbr addresses this might make dead if 485649ba70fb39f2fda4791d255ae8bb373071874fFrits van Bommel/// DeleteDeadConditions is true. 495649ba70fb39f2fda4791d255ae8bb373071874fFrits van Bommelbool ConstantFoldTerminator(BasicBlock *BB, bool DeleteDeadConditions = false); 50148a0bfcea5d3267eac02613cfcc8b5fe8894f2cChris Lattner 51148a0bfcea5d3267eac02613cfcc8b5fe8894f2cChris Lattner//===----------------------------------------------------------------------===// 523481f24c06b3c9de48bdd99c37547471ca8e761eChris Lattner// Local dead code elimination. 53148a0bfcea5d3267eac02613cfcc8b5fe8894f2cChris Lattner// 54148a0bfcea5d3267eac02613cfcc8b5fe8894f2cChris Lattner 553e57cb98bde04a65623493738d51f2a41e601748Chris Lattner/// isInstructionTriviallyDead - Return true if the result produced by the 563e57cb98bde04a65623493738d51f2a41e601748Chris Lattner/// instruction is not used, and the instruction has no side effects. 573e57cb98bde04a65623493738d51f2a41e601748Chris Lattner/// 58148a0bfcea5d3267eac02613cfcc8b5fe8894f2cChris Lattnerbool isInstructionTriviallyDead(Instruction *I); 59148a0bfcea5d3267eac02613cfcc8b5fe8894f2cChris Lattner 603481f24c06b3c9de48bdd99c37547471ca8e761eChris Lattner/// RecursivelyDeleteTriviallyDeadInstructions - If the specified value is a 613481f24c06b3c9de48bdd99c37547471ca8e761eChris Lattner/// trivially dead instruction, delete it. If that makes any of its operands 6290fe0bd68cdbeb980c08628c4992dffad0dc728fDan Gohman/// trivially dead, delete them too, recursively. Return true if any 6390fe0bd68cdbeb980c08628c4992dffad0dc728fDan Gohman/// instructions were deleted. 6490fe0bd68cdbeb980c08628c4992dffad0dc728fDan Gohmanbool RecursivelyDeleteTriviallyDeadInstructions(Value *V); 65afc36a9520971832dfbebc0333593bf5d3098296Dan Gohman 66afc36a9520971832dfbebc0333593bf5d3098296Dan Gohman/// RecursivelyDeleteDeadPHINode - If the specified value is an effectively 67afc36a9520971832dfbebc0333593bf5d3098296Dan Gohman/// dead PHI node, due to being a def-use chain of single-use nodes that 68afc36a9520971832dfbebc0333593bf5d3098296Dan Gohman/// either forms a cycle or is terminated by a trivially dead instruction, 69afc36a9520971832dfbebc0333593bf5d3098296Dan Gohman/// delete it. If that makes any of its operands trivially dead, delete them 702cfbf018a938d14126b9cb10c600e025f9831d2dDuncan Sands/// too, recursively. Return true if a change was made. 7190fe0bd68cdbeb980c08628c4992dffad0dc728fDan Gohmanbool RecursivelyDeleteDeadPHINode(PHINode *PN); 72afc36a9520971832dfbebc0333593bf5d3098296Dan Gohman 73e234a30a282f1aaec4aa63460fe8bba6416832a8Chris Lattner 74e234a30a282f1aaec4aa63460fe8bba6416832a8Chris Lattner/// SimplifyInstructionsInBlock - Scan the specified basic block and try to 75e234a30a282f1aaec4aa63460fe8bba6416832a8Chris Lattner/// simplify any instructions in it and recursively delete dead instructions. 76e234a30a282f1aaec4aa63460fe8bba6416832a8Chris Lattner/// 77e234a30a282f1aaec4aa63460fe8bba6416832a8Chris Lattner/// This returns true if it changed the code, note that it can delete 78e234a30a282f1aaec4aa63460fe8bba6416832a8Chris Lattner/// instructions in other blocks as well in this block. 79e234a30a282f1aaec4aa63460fe8bba6416832a8Chris Lattnerbool SimplifyInstructionsInBlock(BasicBlock *BB, const TargetData *TD = 0); 80e234a30a282f1aaec4aa63460fe8bba6416832a8Chris Lattner 81abbc2dd77908f146f73f4cd1abfdfe47faacf43dChris Lattner//===----------------------------------------------------------------------===// 8229874e0dc6c4e55bc384611273343bb358982cc3Chris Lattner// Control Flow Graph Restructuring. 83148a0bfcea5d3267eac02613cfcc8b5fe8894f2cChris Lattner// 84148a0bfcea5d3267eac02613cfcc8b5fe8894f2cChris Lattner 8540d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner/// RemovePredecessorAndSimplify - Like BasicBlock::removePredecessor, this 8640d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner/// method is called when we're about to delete Pred as a predecessor of BB. If 8740d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner/// BB contains any PHI nodes, this drops the entries in the PHI nodes for Pred. 8840d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner/// 8940d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner/// Unlike the removePredecessor method, this attempts to simplify uses of PHI 9040d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner/// nodes that collapse into identity values. For example, if we have: 9140d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner/// x = phi(1, 0, 0, 0) 9240d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner/// y = and x, z 9340d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner/// 9440d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner/// .. and delete the predecessor corresponding to the '1', this will attempt to 9540d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner/// recursively fold the 'and' to 0. 9640d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattnervoid RemovePredecessorAndSimplify(BasicBlock *BB, BasicBlock *Pred, 9740d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner TargetData *TD = 0); 9840d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner 9940d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner 100b29714a10af94b6daae437e48a82ae32675f79cbChris Lattner/// MergeBasicBlockIntoOnlyPred - BB is a block with one predecessor and its 101b29714a10af94b6daae437e48a82ae32675f79cbChris Lattner/// predecessor is known to have one successor (BB!). Eliminate the edge 102b29714a10af94b6daae437e48a82ae32675f79cbChris Lattner/// between them, moving the instructions in the predecessor into BB. This 103b29714a10af94b6daae437e48a82ae32675f79cbChris Lattner/// deletes the predecessor block. 104b29714a10af94b6daae437e48a82ae32675f79cbChris Lattner/// 105ad80981a106c9d0ec83351e63ee3ac75ed646bf4Andreas Neustiftervoid MergeBasicBlockIntoOnlyPred(BasicBlock *BB, Pass *P = 0); 106b29714a10af94b6daae437e48a82ae32675f79cbChris Lattner 107dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner 108dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner/// TryToSimplifyUncondBranchFromEmptyBlock - BB is known to contain an 109dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner/// unconditional branch, and contains no instructions other than PHI nodes, 110dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner/// potential debug intrinsics and the branch. If possible, eliminate BB by 111dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner/// rewriting all the predecessors to branch to the successor block and return 112dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner/// true. If we can't transform, return false. 113dce94d92df77da125a1c1256a9294db891a9db9cChris Lattnerbool TryToSimplifyUncondBranchFromEmptyBlock(BasicBlock *BB); 11420f4d34fe3609cda8b70956a36080e9cbcd327d4Jim Grosbach 11520f4d34fe3609cda8b70956a36080e9cbcd327d4Jim Grosbach/// EliminateDuplicatePHINodes - Check for and eliminate duplicate PHI 11620f4d34fe3609cda8b70956a36080e9cbcd327d4Jim Grosbach/// nodes in this block. This doesn't try to be clever about PHI nodes 11720f4d34fe3609cda8b70956a36080e9cbcd327d4Jim Grosbach/// which differ only in the order of the incoming values, but instcombine 11820f4d34fe3609cda8b70956a36080e9cbcd327d4Jim Grosbach/// orders them so it usually won't matter. 11920f4d34fe3609cda8b70956a36080e9cbcd327d4Jim Grosbach/// 12020f4d34fe3609cda8b70956a36080e9cbcd327d4Jim Grosbachbool EliminateDuplicatePHINodes(BasicBlock *BB); 12120f4d34fe3609cda8b70956a36080e9cbcd327d4Jim Grosbach 1223e57cb98bde04a65623493738d51f2a41e601748Chris Lattner/// SimplifyCFG - This function is used to do simplification of a CFG. For 1233e57cb98bde04a65623493738d51f2a41e601748Chris Lattner/// example, it adjusts branches to branches to eliminate the extra hop, it 1243e57cb98bde04a65623493738d51f2a41e601748Chris Lattner/// eliminates unreachable basic blocks, and does other "peephole" optimization 1253e57cb98bde04a65623493738d51f2a41e601748Chris Lattner/// of the CFG. It returns true if a modification was made, possibly deleting 1263e57cb98bde04a65623493738d51f2a41e601748Chris Lattner/// the basic block that was pointed to. 1273e57cb98bde04a65623493738d51f2a41e601748Chris Lattner/// 12858e9ee85fda5083e2eea987917e8eab6ba31fe5eJakob Stoklund Olesenbool SimplifyCFG(BasicBlock *BB, const TargetData *TD = 0); 129148a0bfcea5d3267eac02613cfcc8b5fe8894f2cChris Lattner 1304b35f83b91a1a313f0730c600e5178aaf7df98d6Dan Gohman/// FoldBranchToCommonDest - If this basic block is ONLY a setcc and a branch, 1314b35f83b91a1a313f0730c600e5178aaf7df98d6Dan Gohman/// and if a predecessor branches to us and one of our successors, fold the 1324b35f83b91a1a313f0730c600e5178aaf7df98d6Dan Gohman/// setcc into the predecessor and use logical operations to pick the right 1334b35f83b91a1a313f0730c600e5178aaf7df98d6Dan Gohman/// destination. 1344b35f83b91a1a313f0730c600e5178aaf7df98d6Dan Gohmanbool FoldBranchToCommonDest(BranchInst *BI); 1354b35f83b91a1a313f0730c600e5178aaf7df98d6Dan Gohman 136cbf99ee973b6c6e6cd9537dae12031382783bc61Chris Lattner/// DemoteRegToStack - This function takes a virtual register computed by an 1378f71b63bead24de7acc6fe7f0487bf56da22d745Chris Lattner/// Instruction and replaces it with a slot in the stack frame, allocated via 138cbf99ee973b6c6e6cd9537dae12031382783bc61Chris Lattner/// alloca. This allows the CFG to be changed around without fear of 139cbf99ee973b6c6e6cd9537dae12031382783bc61Chris Lattner/// invalidating the SSA information for the value. It returns the pointer to 140cbf99ee973b6c6e6cd9537dae12031382783bc61Chris Lattner/// the alloca inserted to create a stack slot for X. 141cbf99ee973b6c6e6cd9537dae12031382783bc61Chris Lattner/// 14250dead06ffc107edb7e84857baaeeb09039c631cOwen AndersonAllocaInst *DemoteRegToStack(Instruction &X, 1439adc0abad3c3ed40a268ccbcee0c74cb9e1359feOwen Anderson bool VolatileLoads = false, 144d68a07650cdb2e18f18f362ba533459aa10e01b6Dan Gohman Instruction *AllocaPoint = 0); 145cbf99ee973b6c6e6cd9537dae12031382783bc61Chris Lattner 14608d14d2469c4f0465f610a204353220ee2ccde9cTanya Lattner/// DemotePHIToStack - This function takes a virtual register computed by a phi 14708d14d2469c4f0465f610a204353220ee2ccde9cTanya Lattner/// node and replaces it with a slot in the stack frame, allocated via alloca. 14808d14d2469c4f0465f610a204353220ee2ccde9cTanya Lattner/// The phi node is deleted and it returns the pointer to the alloca inserted. 14950dead06ffc107edb7e84857baaeeb09039c631cOwen AndersonAllocaInst *DemotePHIToStack(PHINode *P, Instruction *AllocaPoint = 0); 15008d14d2469c4f0465f610a204353220ee2ccde9cTanya Lattner 151687140c818ba4b896329a83324714140b6580ef8Chris Lattner/// getOrEnforceKnownAlignment - If the specified pointer has an alignment that 152687140c818ba4b896329a83324714140b6580ef8Chris Lattner/// we can determine, return it, otherwise return 0. If PrefAlign is specified, 153687140c818ba4b896329a83324714140b6580ef8Chris Lattner/// and it is more than the alignment of the ultimate object, see if we can 154687140c818ba4b896329a83324714140b6580ef8Chris Lattner/// increase the alignment of the ultimate object, making this check succeed. 155687140c818ba4b896329a83324714140b6580ef8Chris Lattnerunsigned getOrEnforceKnownAlignment(Value *V, unsigned PrefAlign, 156687140c818ba4b896329a83324714140b6580ef8Chris Lattner const TargetData *TD = 0); 157687140c818ba4b896329a83324714140b6580ef8Chris Lattner 158687140c818ba4b896329a83324714140b6580ef8Chris Lattner/// getKnownAlignment - Try to infer an alignment for the specified pointer. 159687140c818ba4b896329a83324714140b6580ef8Chris Lattnerstatic inline unsigned getKnownAlignment(Value *V, const TargetData *TD = 0) { 160687140c818ba4b896329a83324714140b6580ef8Chris Lattner return getOrEnforceKnownAlignment(V, 0, TD); 161687140c818ba4b896329a83324714140b6580ef8Chris Lattner} 162687140c818ba4b896329a83324714140b6580ef8Chris Lattner 1635ee20680c7ebc765950983633e19fafab5235245Devang Patel///===---------------------------------------------------------------------===// 1645ee20680c7ebc765950983633e19fafab5235245Devang Patel/// Dbg Intrinsic utilities 1655ee20680c7ebc765950983633e19fafab5235245Devang Patel/// 1665ee20680c7ebc765950983633e19fafab5235245Devang Patel 1675ee20680c7ebc765950983633e19fafab5235245Devang Patel/// Inserts a llvm.dbg.value instrinsic before the stores to an alloca'd value 1685ee20680c7ebc765950983633e19fafab5235245Devang Patel/// that has an associated llvm.dbg.decl intrinsic. 1695ee20680c7ebc765950983633e19fafab5235245Devang Patelbool ConvertDebugDeclareToDebugValue(DbgDeclareInst *DDI, 1705ee20680c7ebc765950983633e19fafab5235245Devang Patel StoreInst *SI, DIBuilder &Builder); 1715ee20680c7ebc765950983633e19fafab5235245Devang Patel 17236fae67831517f132255118b45b21a8cf199a012Devang Patel/// Inserts a llvm.dbg.value instrinsic before the stores to an alloca'd value 17336fae67831517f132255118b45b21a8cf199a012Devang Patel/// that has an associated llvm.dbg.decl intrinsic. 17436fae67831517f132255118b45b21a8cf199a012Devang Patelbool ConvertDebugDeclareToDebugValue(DbgDeclareInst *DDI, 17536fae67831517f132255118b45b21a8cf199a012Devang Patel LoadInst *LI, DIBuilder &Builder); 17636fae67831517f132255118b45b21a8cf199a012Devang Patel 177813c9a0f19c0d27085a3ea81eb44033747007741Devang Patel/// LowerDbgDeclare - Lowers llvm.dbg.declare intrinsics into appropriate set 178813c9a0f19c0d27085a3ea81eb44033747007741Devang Patel/// of llvm.dbg.value intrinsics. 179813c9a0f19c0d27085a3ea81eb44033747007741Devang Patelbool LowerDbgDeclare(Function &F); 180813c9a0f19c0d27085a3ea81eb44033747007741Devang Patel 181d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke} // End llvm namespace 182d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke 183148a0bfcea5d3267eac02613cfcc8b5fe8894f2cChris Lattner#endif 184