Local.h revision 90fe0bd68cdbeb980c08628c4992dffad0dc728f
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; 224b35f83b91a1a313f0730c600e5178aaf7df98d6Dan Gohmanclass BranchInst; 23d68a07650cdb2e18f18f362ba533459aa10e01b6Dan Gohmanclass Instruction; 24d68a07650cdb2e18f18f362ba533459aa10e01b6Dan Gohmanclass Value; 253e57cb98bde04a65623493738d51f2a41e601748Chris Lattnerclass Pass; 26abbc2dd77908f146f73f4cd1abfdfe47faacf43dChris Lattnerclass PHINode; 27cbf99ee973b6c6e6cd9537dae12031382783bc61Chris Lattnerclass AllocaInst; 28c5406b55d58b7378822d5f6316bf253ad0eaa82dChris Lattnerclass ConstantExpr; 2973259caa44b03206d1e39e77a7aa375250b1a193Chris Lattnerclass TargetData; 3043fd9017a69a6314fb780f87083b1e57a0981287Chris Lattnerclass DbgInfoIntrinsic; 31d68a07650cdb2e18f18f362ba533459aa10e01b6Dan Gohman 32d68a07650cdb2e18f18f362ba533459aa10e01b6Dan Gohmantemplate<typename T> class SmallVectorImpl; 334f02c74a8ec8f4ca83b823146cb9c987ed79b3f6Chris Lattner 34148a0bfcea5d3267eac02613cfcc8b5fe8894f2cChris Lattner//===----------------------------------------------------------------------===// 356cc8a93c486f889c5767278508bc655942ba408eChris Lattner// Local analysis. 366cc8a93c486f889c5767278508bc655942ba408eChris Lattner// 376cc8a93c486f889c5767278508bc655942ba408eChris Lattner 386cc8a93c486f889c5767278508bc655942ba408eChris Lattner/// isSafeToLoadUnconditionally - Return true if we know that executing a load 396cc8a93c486f889c5767278508bc655942ba408eChris Lattner/// from this value cannot trap. If it is not obviously safe to load from the 406cc8a93c486f889c5767278508bc655942ba408eChris Lattner/// specified pointer, we do a quick local scan of the basic block containing 416cc8a93c486f889c5767278508bc655942ba408eChris Lattner/// ScanFrom, to determine if the address is already accessed. 426cc8a93c486f889c5767278508bc655942ba408eChris Lattnerbool isSafeToLoadUnconditionally(Value *V, Instruction *ScanFrom); 436cc8a93c486f889c5767278508bc655942ba408eChris Lattner 446cc8a93c486f889c5767278508bc655942ba408eChris Lattner//===----------------------------------------------------------------------===// 453481f24c06b3c9de48bdd99c37547471ca8e761eChris Lattner// Local constant propagation. 46148a0bfcea5d3267eac02613cfcc8b5fe8894f2cChris Lattner// 47148a0bfcea5d3267eac02613cfcc8b5fe8894f2cChris Lattner 483e57cb98bde04a65623493738d51f2a41e601748Chris Lattner/// ConstantFoldTerminator - If a terminator instruction is predicated on a 493e57cb98bde04a65623493738d51f2a41e601748Chris Lattner/// constant value, convert it into an unconditional branch to the constant 503e57cb98bde04a65623493738d51f2a41e601748Chris Lattner/// destination. This is a nontrivial operation because the successors of this 513e57cb98bde04a65623493738d51f2a41e601748Chris Lattner/// basic block must have their PHI nodes updated. 523e57cb98bde04a65623493738d51f2a41e601748Chris Lattner/// 5376ae3445f81164aaff9f95123426109c119f27c0Chris Lattnerbool ConstantFoldTerminator(BasicBlock *BB); 54148a0bfcea5d3267eac02613cfcc8b5fe8894f2cChris Lattner 55148a0bfcea5d3267eac02613cfcc8b5fe8894f2cChris Lattner//===----------------------------------------------------------------------===// 563481f24c06b3c9de48bdd99c37547471ca8e761eChris Lattner// Local dead code elimination. 57148a0bfcea5d3267eac02613cfcc8b5fe8894f2cChris Lattner// 58148a0bfcea5d3267eac02613cfcc8b5fe8894f2cChris Lattner 593e57cb98bde04a65623493738d51f2a41e601748Chris Lattner/// isInstructionTriviallyDead - Return true if the result produced by the 603e57cb98bde04a65623493738d51f2a41e601748Chris Lattner/// instruction is not used, and the instruction has no side effects. 613e57cb98bde04a65623493738d51f2a41e601748Chris Lattner/// 62148a0bfcea5d3267eac02613cfcc8b5fe8894f2cChris Lattnerbool isInstructionTriviallyDead(Instruction *I); 63148a0bfcea5d3267eac02613cfcc8b5fe8894f2cChris Lattner 643481f24c06b3c9de48bdd99c37547471ca8e761eChris Lattner/// RecursivelyDeleteTriviallyDeadInstructions - If the specified value is a 653481f24c06b3c9de48bdd99c37547471ca8e761eChris Lattner/// trivially dead instruction, delete it. If that makes any of its operands 6690fe0bd68cdbeb980c08628c4992dffad0dc728fDan Gohman/// trivially dead, delete them too, recursively. Return true if any 6790fe0bd68cdbeb980c08628c4992dffad0dc728fDan Gohman/// instructions were deleted. 6890fe0bd68cdbeb980c08628c4992dffad0dc728fDan Gohmanbool RecursivelyDeleteTriviallyDeadInstructions(Value *V); 69afc36a9520971832dfbebc0333593bf5d3098296Dan Gohman 70afc36a9520971832dfbebc0333593bf5d3098296Dan Gohman/// RecursivelyDeleteDeadPHINode - If the specified value is an effectively 71afc36a9520971832dfbebc0333593bf5d3098296Dan Gohman/// dead PHI node, due to being a def-use chain of single-use nodes that 72afc36a9520971832dfbebc0333593bf5d3098296Dan Gohman/// either forms a cycle or is terminated by a trivially dead instruction, 73afc36a9520971832dfbebc0333593bf5d3098296Dan Gohman/// delete it. If that makes any of its operands trivially dead, delete them 7490fe0bd68cdbeb980c08628c4992dffad0dc728fDan Gohman/// too, recursively. Return true if the PHI node is actually deleted. 7590fe0bd68cdbeb980c08628c4992dffad0dc728fDan Gohmanbool RecursivelyDeleteDeadPHINode(PHINode *PN); 76afc36a9520971832dfbebc0333593bf5d3098296Dan Gohman 77abbc2dd77908f146f73f4cd1abfdfe47faacf43dChris Lattner//===----------------------------------------------------------------------===// 7829874e0dc6c4e55bc384611273343bb358982cc3Chris Lattner// Control Flow Graph Restructuring. 79148a0bfcea5d3267eac02613cfcc8b5fe8894f2cChris Lattner// 80148a0bfcea5d3267eac02613cfcc8b5fe8894f2cChris Lattner 8140d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner/// RemovePredecessorAndSimplify - Like BasicBlock::removePredecessor, this 8240d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner/// method is called when we're about to delete Pred as a predecessor of BB. If 8340d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner/// BB contains any PHI nodes, this drops the entries in the PHI nodes for Pred. 8440d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner/// 8540d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner/// Unlike the removePredecessor method, this attempts to simplify uses of PHI 8640d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner/// nodes that collapse into identity values. For example, if we have: 8740d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner/// x = phi(1, 0, 0, 0) 8840d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner/// y = and x, z 8940d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner/// 9040d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner/// .. and delete the predecessor corresponding to the '1', this will attempt to 9140d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner/// recursively fold the 'and' to 0. 9240d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattnervoid RemovePredecessorAndSimplify(BasicBlock *BB, BasicBlock *Pred, 9340d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner TargetData *TD = 0); 9440d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner 9540d8c28b27377199b7465ba2c5a2c59c6fd12fa9Chris Lattner 96b29714a10af94b6daae437e48a82ae32675f79cbChris Lattner/// MergeBasicBlockIntoOnlyPred - BB is a block with one predecessor and its 97b29714a10af94b6daae437e48a82ae32675f79cbChris Lattner/// predecessor is known to have one successor (BB!). Eliminate the edge 98b29714a10af94b6daae437e48a82ae32675f79cbChris Lattner/// between them, moving the instructions in the predecessor into BB. This 99b29714a10af94b6daae437e48a82ae32675f79cbChris Lattner/// deletes the predecessor block. 100b29714a10af94b6daae437e48a82ae32675f79cbChris Lattner/// 101ad80981a106c9d0ec83351e63ee3ac75ed646bf4Andreas Neustiftervoid MergeBasicBlockIntoOnlyPred(BasicBlock *BB, Pass *P = 0); 102b29714a10af94b6daae437e48a82ae32675f79cbChris Lattner 103dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner 104dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner/// TryToSimplifyUncondBranchFromEmptyBlock - BB is known to contain an 105dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner/// unconditional branch, and contains no instructions other than PHI nodes, 106dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner/// potential debug intrinsics and the branch. If possible, eliminate BB by 107dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner/// rewriting all the predecessors to branch to the successor block and return 108dce94d92df77da125a1c1256a9294db891a9db9cChris Lattner/// true. If we can't transform, return false. 109dce94d92df77da125a1c1256a9294db891a9db9cChris Lattnerbool TryToSimplifyUncondBranchFromEmptyBlock(BasicBlock *BB); 11020f4d34fe3609cda8b70956a36080e9cbcd327d4Jim Grosbach 11120f4d34fe3609cda8b70956a36080e9cbcd327d4Jim Grosbach/// EliminateDuplicatePHINodes - Check for and eliminate duplicate PHI 11220f4d34fe3609cda8b70956a36080e9cbcd327d4Jim Grosbach/// nodes in this block. This doesn't try to be clever about PHI nodes 11320f4d34fe3609cda8b70956a36080e9cbcd327d4Jim Grosbach/// which differ only in the order of the incoming values, but instcombine 11420f4d34fe3609cda8b70956a36080e9cbcd327d4Jim Grosbach/// orders them so it usually won't matter. 11520f4d34fe3609cda8b70956a36080e9cbcd327d4Jim Grosbach/// 11620f4d34fe3609cda8b70956a36080e9cbcd327d4Jim Grosbachbool EliminateDuplicatePHINodes(BasicBlock *BB); 11720f4d34fe3609cda8b70956a36080e9cbcd327d4Jim Grosbach 1183e57cb98bde04a65623493738d51f2a41e601748Chris Lattner/// SimplifyCFG - This function is used to do simplification of a CFG. For 1193e57cb98bde04a65623493738d51f2a41e601748Chris Lattner/// example, it adjusts branches to branches to eliminate the extra hop, it 1203e57cb98bde04a65623493738d51f2a41e601748Chris Lattner/// eliminates unreachable basic blocks, and does other "peephole" optimization 1213e57cb98bde04a65623493738d51f2a41e601748Chris Lattner/// of the CFG. It returns true if a modification was made, possibly deleting 1223e57cb98bde04a65623493738d51f2a41e601748Chris Lattner/// the basic block that was pointed to. 1233e57cb98bde04a65623493738d51f2a41e601748Chris Lattner/// 1243e57cb98bde04a65623493738d51f2a41e601748Chris Lattner/// WARNING: The entry node of a method may not be simplified. 1253e57cb98bde04a65623493738d51f2a41e601748Chris Lattner/// 12618961504fc2b299578dba817900a0696cf3ccc4dChris Lattnerbool SimplifyCFG(BasicBlock *BB); 127148a0bfcea5d3267eac02613cfcc8b5fe8894f2cChris Lattner 1284b35f83b91a1a313f0730c600e5178aaf7df98d6Dan Gohman/// FoldBranchToCommonDest - If this basic block is ONLY a setcc and a branch, 1294b35f83b91a1a313f0730c600e5178aaf7df98d6Dan Gohman/// and if a predecessor branches to us and one of our successors, fold the 1304b35f83b91a1a313f0730c600e5178aaf7df98d6Dan Gohman/// setcc into the predecessor and use logical operations to pick the right 1314b35f83b91a1a313f0730c600e5178aaf7df98d6Dan Gohman/// destination. 1324b35f83b91a1a313f0730c600e5178aaf7df98d6Dan Gohmanbool FoldBranchToCommonDest(BranchInst *BI); 1334b35f83b91a1a313f0730c600e5178aaf7df98d6Dan Gohman 134cbf99ee973b6c6e6cd9537dae12031382783bc61Chris Lattner/// DemoteRegToStack - This function takes a virtual register computed by an 1358f71b63bead24de7acc6fe7f0487bf56da22d745Chris Lattner/// Instruction and replaces it with a slot in the stack frame, allocated via 136cbf99ee973b6c6e6cd9537dae12031382783bc61Chris Lattner/// alloca. This allows the CFG to be changed around without fear of 137cbf99ee973b6c6e6cd9537dae12031382783bc61Chris Lattner/// invalidating the SSA information for the value. It returns the pointer to 138cbf99ee973b6c6e6cd9537dae12031382783bc61Chris Lattner/// the alloca inserted to create a stack slot for X. 139cbf99ee973b6c6e6cd9537dae12031382783bc61Chris Lattner/// 14050dead06ffc107edb7e84857baaeeb09039c631cOwen AndersonAllocaInst *DemoteRegToStack(Instruction &X, 1419adc0abad3c3ed40a268ccbcee0c74cb9e1359feOwen Anderson bool VolatileLoads = false, 142d68a07650cdb2e18f18f362ba533459aa10e01b6Dan Gohman Instruction *AllocaPoint = 0); 143cbf99ee973b6c6e6cd9537dae12031382783bc61Chris Lattner 14408d14d2469c4f0465f610a204353220ee2ccde9cTanya Lattner/// DemotePHIToStack - This function takes a virtual register computed by a phi 14508d14d2469c4f0465f610a204353220ee2ccde9cTanya Lattner/// node and replaces it with a slot in the stack frame, allocated via alloca. 14608d14d2469c4f0465f610a204353220ee2ccde9cTanya Lattner/// The phi node is deleted and it returns the pointer to the alloca inserted. 14750dead06ffc107edb7e84857baaeeb09039c631cOwen AndersonAllocaInst *DemotePHIToStack(PHINode *P, Instruction *AllocaPoint = 0); 14808d14d2469c4f0465f610a204353220ee2ccde9cTanya Lattner 1494afc90dacf309999d8b7f6c2b4b0c56af346bab5Devang Patel/// OnlyUsedByDbgIntrinsics - Return true if the instruction I is only used 1504afc90dacf309999d8b7f6c2b4b0c56af346bab5Devang Patel/// by DbgIntrinsics. If DbgInUses is specified then the vector is filled 1514afc90dacf309999d8b7f6c2b4b0c56af346bab5Devang Patel/// with DbgInfoIntrinsic that use the instruction I. 1524afc90dacf309999d8b7f6c2b4b0c56af346bab5Devang Patelbool OnlyUsedByDbgInfoIntrinsics(Instruction *I, 1534afc90dacf309999d8b7f6c2b4b0c56af346bab5Devang Patel SmallVectorImpl<DbgInfoIntrinsic *> *DbgInUses = 0); 154c79e1182470ed12f1f3d0d35c1725366519a9af7Devang Patel 155d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke} // End llvm namespace 156d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke 157148a0bfcea5d3267eac02613cfcc8b5fe8894f2cChris Lattner#endif 158