Local.h revision 6cc8a93c486f889c5767278508bc655942ba408e
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; 22d68a07650cdb2e18f18f362ba533459aa10e01b6Dan Gohmanclass Instruction; 23d68a07650cdb2e18f18f362ba533459aa10e01b6Dan Gohmanclass Value; 243e57cb98bde04a65623493738d51f2a41e601748Chris Lattnerclass Pass; 25abbc2dd77908f146f73f4cd1abfdfe47faacf43dChris Lattnerclass PHINode; 26cbf99ee973b6c6e6cd9537dae12031382783bc61Chris Lattnerclass AllocaInst; 27c5406b55d58b7378822d5f6316bf253ad0eaa82dChris Lattnerclass ConstantExpr; 2873259caa44b03206d1e39e77a7aa375250b1a193Chris Lattnerclass TargetData; 29aff9c270de8de7d1a0bc138d391bc67136bad58eCedric Venetstruct DbgInfoIntrinsic; 30d68a07650cdb2e18f18f362ba533459aa10e01b6Dan Gohman 31d68a07650cdb2e18f18f362ba533459aa10e01b6Dan Gohmantemplate<typename T> class SmallVectorImpl; 324f02c74a8ec8f4ca83b823146cb9c987ed79b3f6Chris Lattner 33148a0bfcea5d3267eac02613cfcc8b5fe8894f2cChris Lattner//===----------------------------------------------------------------------===// 346cc8a93c486f889c5767278508bc655942ba408eChris Lattner// Local analysis. 356cc8a93c486f889c5767278508bc655942ba408eChris Lattner// 366cc8a93c486f889c5767278508bc655942ba408eChris Lattner 376cc8a93c486f889c5767278508bc655942ba408eChris Lattner/// isSafeToLoadUnconditionally - Return true if we know that executing a load 386cc8a93c486f889c5767278508bc655942ba408eChris Lattner/// from this value cannot trap. If it is not obviously safe to load from the 396cc8a93c486f889c5767278508bc655942ba408eChris Lattner/// specified pointer, we do a quick local scan of the basic block containing 406cc8a93c486f889c5767278508bc655942ba408eChris Lattner/// ScanFrom, to determine if the address is already accessed. 416cc8a93c486f889c5767278508bc655942ba408eChris Lattnerbool isSafeToLoadUnconditionally(Value *V, Instruction *ScanFrom); 426cc8a93c486f889c5767278508bc655942ba408eChris Lattner 436cc8a93c486f889c5767278508bc655942ba408eChris Lattner//===----------------------------------------------------------------------===// 443481f24c06b3c9de48bdd99c37547471ca8e761eChris Lattner// Local constant propagation. 45148a0bfcea5d3267eac02613cfcc8b5fe8894f2cChris Lattner// 46148a0bfcea5d3267eac02613cfcc8b5fe8894f2cChris Lattner 473e57cb98bde04a65623493738d51f2a41e601748Chris Lattner/// ConstantFoldTerminator - If a terminator instruction is predicated on a 483e57cb98bde04a65623493738d51f2a41e601748Chris Lattner/// constant value, convert it into an unconditional branch to the constant 493e57cb98bde04a65623493738d51f2a41e601748Chris Lattner/// destination. This is a nontrivial operation because the successors of this 503e57cb98bde04a65623493738d51f2a41e601748Chris Lattner/// basic block must have their PHI nodes updated. 513e57cb98bde04a65623493738d51f2a41e601748Chris Lattner/// 5276ae3445f81164aaff9f95123426109c119f27c0Chris Lattnerbool ConstantFoldTerminator(BasicBlock *BB); 53148a0bfcea5d3267eac02613cfcc8b5fe8894f2cChris Lattner 54148a0bfcea5d3267eac02613cfcc8b5fe8894f2cChris Lattner//===----------------------------------------------------------------------===// 553481f24c06b3c9de48bdd99c37547471ca8e761eChris Lattner// Local dead code elimination. 56148a0bfcea5d3267eac02613cfcc8b5fe8894f2cChris Lattner// 57148a0bfcea5d3267eac02613cfcc8b5fe8894f2cChris Lattner 583e57cb98bde04a65623493738d51f2a41e601748Chris Lattner/// isInstructionTriviallyDead - Return true if the result produced by the 593e57cb98bde04a65623493738d51f2a41e601748Chris Lattner/// instruction is not used, and the instruction has no side effects. 603e57cb98bde04a65623493738d51f2a41e601748Chris Lattner/// 61148a0bfcea5d3267eac02613cfcc8b5fe8894f2cChris Lattnerbool isInstructionTriviallyDead(Instruction *I); 62148a0bfcea5d3267eac02613cfcc8b5fe8894f2cChris Lattner 633481f24c06b3c9de48bdd99c37547471ca8e761eChris Lattner/// RecursivelyDeleteTriviallyDeadInstructions - If the specified value is a 643481f24c06b3c9de48bdd99c37547471ca8e761eChris Lattner/// trivially dead instruction, delete it. If that makes any of its operands 653481f24c06b3c9de48bdd99c37547471ca8e761eChris Lattner/// trivially dead, delete them too, recursively. 6635738ac150afafe2359268d4b2169498c6c98c5fDan Gohmanvoid RecursivelyDeleteTriviallyDeadInstructions(Value *V); 67afc36a9520971832dfbebc0333593bf5d3098296Dan Gohman 68afc36a9520971832dfbebc0333593bf5d3098296Dan Gohman/// RecursivelyDeleteDeadPHINode - If the specified value is an effectively 69afc36a9520971832dfbebc0333593bf5d3098296Dan Gohman/// dead PHI node, due to being a def-use chain of single-use nodes that 70afc36a9520971832dfbebc0333593bf5d3098296Dan Gohman/// either forms a cycle or is terminated by a trivially dead instruction, 71afc36a9520971832dfbebc0333593bf5d3098296Dan Gohman/// delete it. If that makes any of its operands trivially dead, delete them 72afc36a9520971832dfbebc0333593bf5d3098296Dan Gohman/// too, recursively. 7335738ac150afafe2359268d4b2169498c6c98c5fDan Gohmanvoid RecursivelyDeleteDeadPHINode(PHINode *PN); 74afc36a9520971832dfbebc0333593bf5d3098296Dan Gohman 75abbc2dd77908f146f73f4cd1abfdfe47faacf43dChris Lattner//===----------------------------------------------------------------------===// 7629874e0dc6c4e55bc384611273343bb358982cc3Chris Lattner// Control Flow Graph Restructuring. 77148a0bfcea5d3267eac02613cfcc8b5fe8894f2cChris Lattner// 78148a0bfcea5d3267eac02613cfcc8b5fe8894f2cChris Lattner 79b29714a10af94b6daae437e48a82ae32675f79cbChris Lattner/// MergeBasicBlockIntoOnlyPred - BB is a block with one predecessor and its 80b29714a10af94b6daae437e48a82ae32675f79cbChris Lattner/// predecessor is known to have one successor (BB!). Eliminate the edge 81b29714a10af94b6daae437e48a82ae32675f79cbChris Lattner/// between them, moving the instructions in the predecessor into BB. This 82b29714a10af94b6daae437e48a82ae32675f79cbChris Lattner/// deletes the predecessor block. 83b29714a10af94b6daae437e48a82ae32675f79cbChris Lattner/// 84b29714a10af94b6daae437e48a82ae32675f79cbChris Lattnervoid MergeBasicBlockIntoOnlyPred(BasicBlock *BB); 85b29714a10af94b6daae437e48a82ae32675f79cbChris Lattner 86b29714a10af94b6daae437e48a82ae32675f79cbChris Lattner 873e57cb98bde04a65623493738d51f2a41e601748Chris Lattner/// SimplifyCFG - This function is used to do simplification of a CFG. For 883e57cb98bde04a65623493738d51f2a41e601748Chris Lattner/// example, it adjusts branches to branches to eliminate the extra hop, it 893e57cb98bde04a65623493738d51f2a41e601748Chris Lattner/// eliminates unreachable basic blocks, and does other "peephole" optimization 903e57cb98bde04a65623493738d51f2a41e601748Chris Lattner/// of the CFG. It returns true if a modification was made, possibly deleting 913e57cb98bde04a65623493738d51f2a41e601748Chris Lattner/// the basic block that was pointed to. 923e57cb98bde04a65623493738d51f2a41e601748Chris Lattner/// 933e57cb98bde04a65623493738d51f2a41e601748Chris Lattner/// WARNING: The entry node of a method may not be simplified. 943e57cb98bde04a65623493738d51f2a41e601748Chris Lattner/// 9518961504fc2b299578dba817900a0696cf3ccc4dChris Lattnerbool SimplifyCFG(BasicBlock *BB); 96148a0bfcea5d3267eac02613cfcc8b5fe8894f2cChris Lattner 97cbf99ee973b6c6e6cd9537dae12031382783bc61Chris Lattner/// DemoteRegToStack - This function takes a virtual register computed by an 988f71b63bead24de7acc6fe7f0487bf56da22d745Chris Lattner/// Instruction and replaces it with a slot in the stack frame, allocated via 99cbf99ee973b6c6e6cd9537dae12031382783bc61Chris Lattner/// alloca. This allows the CFG to be changed around without fear of 100cbf99ee973b6c6e6cd9537dae12031382783bc61Chris Lattner/// invalidating the SSA information for the value. It returns the pointer to 101cbf99ee973b6c6e6cd9537dae12031382783bc61Chris Lattner/// the alloca inserted to create a stack slot for X. 102cbf99ee973b6c6e6cd9537dae12031382783bc61Chris Lattner/// 103a024e8cedab928403818ac79e899cdae2a356c56Anton KorobeynikovAllocaInst *DemoteRegToStack(Instruction &X, bool VolatileLoads = false, 104d68a07650cdb2e18f18f362ba533459aa10e01b6Dan Gohman Instruction *AllocaPoint = 0); 105cbf99ee973b6c6e6cd9537dae12031382783bc61Chris Lattner 10608d14d2469c4f0465f610a204353220ee2ccde9cTanya Lattner/// DemotePHIToStack - This function takes a virtual register computed by a phi 10708d14d2469c4f0465f610a204353220ee2ccde9cTanya Lattner/// node and replaces it with a slot in the stack frame, allocated via alloca. 10808d14d2469c4f0465f610a204353220ee2ccde9cTanya Lattner/// The phi node is deleted and it returns the pointer to the alloca inserted. 109d68a07650cdb2e18f18f362ba533459aa10e01b6Dan GohmanAllocaInst *DemotePHIToStack(PHINode *P, Instruction *AllocaPoint = 0); 11008d14d2469c4f0465f610a204353220ee2ccde9cTanya Lattner 1114afc90dacf309999d8b7f6c2b4b0c56af346bab5Devang Patel/// OnlyUsedByDbgIntrinsics - Return true if the instruction I is only used 1124afc90dacf309999d8b7f6c2b4b0c56af346bab5Devang Patel/// by DbgIntrinsics. If DbgInUses is specified then the vector is filled 1134afc90dacf309999d8b7f6c2b4b0c56af346bab5Devang Patel/// with DbgInfoIntrinsic that use the instruction I. 1144afc90dacf309999d8b7f6c2b4b0c56af346bab5Devang Patelbool OnlyUsedByDbgInfoIntrinsics(Instruction *I, 1154afc90dacf309999d8b7f6c2b4b0c56af346bab5Devang Patel SmallVectorImpl<DbgInfoIntrinsic *> *DbgInUses = 0); 116c79e1182470ed12f1f3d0d35c1725366519a9af7Devang Patel 117c79e1182470ed12f1f3d0d35c1725366519a9af7Devang Patel/// UserIsDebugInfo - Return true if U is a constant expr used by 118c79e1182470ed12f1f3d0d35c1725366519a9af7Devang Patel/// llvm.dbg.variable or llvm.dbg.global_variable 119c79e1182470ed12f1f3d0d35c1725366519a9af7Devang Patelbool UserIsDebugInfo(User *U); 120c79e1182470ed12f1f3d0d35c1725366519a9af7Devang Patel 121c79e1182470ed12f1f3d0d35c1725366519a9af7Devang Patel/// RemoveDbgInfoUser - Remove an User which is representing debug info. 122c79e1182470ed12f1f3d0d35c1725366519a9af7Devang Patelvoid RemoveDbgInfoUser(User *U); 123c79e1182470ed12f1f3d0d35c1725366519a9af7Devang Patel 124d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke} // End llvm namespace 125d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke 126148a0bfcea5d3267eac02613cfcc8b5fe8894f2cChris Lattner#endif 127