SCCP.cpp revision 05bb789430bab7d8fae1e94fb9aa0bb21e679ebf
1afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg//===- SCCP.cpp - Sparse Conditional Constant Propagation -----------------===// 2be1b8e5d6c6692010a3ec117035d9b218929e2b3Brian// 325be043940b25a5fe6eb8058070b3e8f12e92039Brian Paul// The LLVM Compiler Infrastructure 45e3bc0c2a2bcdf59949410f94c9b705fc1281ce8Jouk Jansen// 59f6022d0567dc1288888212d7128acc48795b306Brian// This file is distributed under the University of Illinois Open Source 680684649a6d01f0e0517b14f61cbcad6fa101929Brian Paul// License. See LICENSE.TXT for details. 75e3bc0c2a2bcdf59949410f94c9b705fc1281ce8Jouk Jansen// 8afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg//===----------------------------------------------------------------------===// 9afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg// 10afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg// This file implements sparse conditional constant propagation and merging: 11afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg// 12afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg// Specifically, this: 13afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg// * Assumes values are constant unless proven otherwise 145e3bc0c2a2bcdf59949410f94c9b705fc1281ce8Jouk Jansen// * Assumes BasicBlocks are dead unless proven otherwise 15afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg// * Proves values to be constant, and replaces them with constants 16afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg// * Proves conditional branches to be unconditional 175e3bc0c2a2bcdf59949410f94c9b705fc1281ce8Jouk Jansen// 18afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg// Notice that: 19afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg// * This pass has a habit of making definitions be dead. It is a good idea 20afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg// to to run a DCE pass sometime after running this pass. 21afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg// 22afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg//===----------------------------------------------------------------------===// 23afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg 24afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg#define DEBUG_TYPE "sccp" 25afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg#include "llvm/Transforms/Scalar.h" 266dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell#include "llvm/Transforms/IPO.h" 27add9f2168a5d6b15eb9955ee761246c4f4cf8458Brian Paul#include "llvm/Constants.h" 28add9f2168a5d6b15eb9955ee761246c4f4cf8458Brian Paul#include "llvm/DerivedTypes.h" 2977ee31930a1b0cc7766939415f4f04ed6a1fa4acBrian Paul#include "llvm/Instructions.h" 30add9f2168a5d6b15eb9955ee761246c4f4cf8458Brian Paul#include "llvm/Pass.h" 31add9f2168a5d6b15eb9955ee761246c4f4cf8458Brian Paul#include "llvm/Analysis/ConstantFolding.h" 32add9f2168a5d6b15eb9955ee761246c4f4cf8458Brian Paul#include "llvm/Transforms/Utils/Local.h" 33fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul#include "llvm/Support/CallSite.h" 34b46712ca9d379d9c091f5543500088d82cf9776cBrian Paul#include "llvm/Support/Compiler.h" 35afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg#include "llvm/Support/Debug.h" 36d100cbf721010f4eacc87507cc87c5314150d493Maciej Cencora#include "llvm/Support/InstVisitor.h" 3734bd1233a9874fe12a822c4fcb926d48456e1f29Brian Paul#include "llvm/ADT/DenseMap.h" 3834bd1233a9874fe12a822c4fcb926d48456e1f29Brian Paul#include "llvm/ADT/SmallSet.h" 392897cee99fb877e1f3cd9a881a61418c9c31867fBrian Paul#include "llvm/ADT/SmallVector.h" 40afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg#include "llvm/ADT/Statistic.h" 413c63452e64df7e10aa073c6c3b9492b1d7dabbb8Brian Paul#include "llvm/ADT/STLExtras.h" 42ebb248aa5c018dc676d389221d76ed329059789eBrian Paul#include <algorithm> 43fa4525e289b475b928a7b2c4055af9dd7fe46600Brian Paulusing namespace llvm; 4489fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul 451a2bb37264b4448d33f2948fe1702c9dc936395dBrian PaulSTATISTIC(NumInstRemoved, "Number of instructions removed"); 46afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtgSTATISTIC(NumDeadBlocks , "Number of basic blocks unreachable"); 47afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg 487179a822628963d8cfa0817cf072c5acb70638a7Kristian HøgsbergSTATISTIC(IPNumInstRemoved, "Number ofinstructions removed by IPSCCP"); 495e3bc0c2a2bcdf59949410f94c9b705fc1281ce8Jouk JansenSTATISTIC(IPNumDeadBlocks , "Number of basic blocks unreachable by IPSCCP"); 50afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtgSTATISTIC(IPNumArgsElimed ,"Number of arguments constant propagated by IPSCCP"); 51afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtgSTATISTIC(IPNumGlobalConst, "Number of globals found to be constant by IPSCCP"); 524cf6718725c7cf3bfb728118a8b14f8cf206c701Brian Paul 5363f01309801c5a900d8d7f5ccd63413e33ff9bffBrian Paulnamespace { 5463f01309801c5a900d8d7f5ccd63413e33ff9bffBrian Paul/// LatticeVal class - This class represents the different lattice values that 5563f01309801c5a900d8d7f5ccd63413e33ff9bffBrian Paul/// an LLVM value may occupy. It is a simple class with value semantics. 5663f01309801c5a900d8d7f5ccd63413e33ff9bffBrian Paul/// 5763f01309801c5a900d8d7f5ccd63413e33ff9bffBrian Paulclass VISIBILITY_HIDDEN LatticeVal { 5863f01309801c5a900d8d7f5ccd63413e33ff9bffBrian Paul enum { 5963f01309801c5a900d8d7f5ccd63413e33ff9bffBrian Paul /// undefined - This LLVM Value has no known value yet. 6063f01309801c5a900d8d7f5ccd63413e33ff9bffBrian Paul undefined, 6163f01309801c5a900d8d7f5ccd63413e33ff9bffBrian Paul 6263f01309801c5a900d8d7f5ccd63413e33ff9bffBrian Paul /// constant - This LLVM Value has a specific constant value. 6363f01309801c5a900d8d7f5ccd63413e33ff9bffBrian Paul constant, 644cf6718725c7cf3bfb728118a8b14f8cf206c701Brian Paul 654cf6718725c7cf3bfb728118a8b14f8cf206c701Brian Paul /// forcedconstant - This LLVM Value was thought to be undef until 664cf6718725c7cf3bfb728118a8b14f8cf206c701Brian Paul /// ResolvedUndefsIn. This is treated just like 'constant', but if merged 674cf6718725c7cf3bfb728118a8b14f8cf206c701Brian Paul /// with another (different) constant, it goes to overdefined, instead of 684cf6718725c7cf3bfb728118a8b14f8cf206c701Brian Paul /// asserting. 694cf6718725c7cf3bfb728118a8b14f8cf206c701Brian Paul forcedconstant, 704cf6718725c7cf3bfb728118a8b14f8cf206c701Brian Paul 714cf6718725c7cf3bfb728118a8b14f8cf206c701Brian Paul /// overdefined - This instruction is not known to be constant, and we know 724cf6718725c7cf3bfb728118a8b14f8cf206c701Brian Paul /// it has a value. 734cf6718725c7cf3bfb728118a8b14f8cf206c701Brian Paul overdefined 744cf6718725c7cf3bfb728118a8b14f8cf206c701Brian Paul } LatticeValue; // The current lattice position 754cf6718725c7cf3bfb728118a8b14f8cf206c701Brian Paul 764cf6718725c7cf3bfb728118a8b14f8cf206c701Brian Paul Constant *ConstantVal; // If Constant value, the current value 774cf6718725c7cf3bfb728118a8b14f8cf206c701Brian Paulpublic: 784cf6718725c7cf3bfb728118a8b14f8cf206c701Brian Paul inline LatticeVal() : LatticeValue(undefined), ConstantVal(0) {} 794cf6718725c7cf3bfb728118a8b14f8cf206c701Brian Paul 804cf6718725c7cf3bfb728118a8b14f8cf206c701Brian Paul // markOverdefined - Return true if this is a new status to be in... 814cf6718725c7cf3bfb728118a8b14f8cf206c701Brian Paul inline bool markOverdefined() { 824cf6718725c7cf3bfb728118a8b14f8cf206c701Brian Paul if (LatticeValue != overdefined) { 834cf6718725c7cf3bfb728118a8b14f8cf206c701Brian Paul LatticeValue = overdefined; 84f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul return true; 85887e2cf01a99f7fe1b7c94320b7bdbbf0d6ad2f8Brian Paul } 86973da83f6237b5af4a9ee77f32fdfa5c04ecabc8Brian Paul return false; 87afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg } 88fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul 89fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul // markConstant - Return true if this is a new status for us. 90afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg inline bool markConstant(Constant *V) { 91afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg if (LatticeValue != constant) { 92afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg if (LatticeValue == undefined) { 93afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg LatticeValue = constant; 94973da83f6237b5af4a9ee77f32fdfa5c04ecabc8Brian Paul assert(V && "Marking constant with NULL"); 95afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg ConstantVal = V; 96973da83f6237b5af4a9ee77f32fdfa5c04ecabc8Brian Paul } else { 97973da83f6237b5af4a9ee77f32fdfa5c04ecabc8Brian Paul assert(LatticeValue == forcedconstant && 98973da83f6237b5af4a9ee77f32fdfa5c04ecabc8Brian Paul "Cannot move from overdefined to constant!"); 99afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg // Stay at forcedconstant if the constant is the same. 100afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg if (V == ConstantVal) return false; 101afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg 102afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg // Otherwise, we go to overdefined. Assumptions made based on the 103afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg // forced value are possibly wrong. Assuming this is another constant 104afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg // could expose a contradiction. 105887e2cf01a99f7fe1b7c94320b7bdbbf0d6ad2f8Brian Paul LatticeValue = overdefined; 106afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg } 107afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg return true; 108afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg } else { 109afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg assert(ConstantVal == V && "Marking constant with different value"); 110afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg } 111afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg return false; 112afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg } 113afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg 1146dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell inline void markForcedConstant(Constant *V) { 1151749a25ca889d514889b34cf6311c8014d97bf66Brian Paul assert(LatticeValue == undefined && "Can't force a defined value!"); 1161749a25ca889d514889b34cf6311c8014d97bf66Brian Paul LatticeValue = forcedconstant; 1176dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell ConstantVal = V; 1186dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell } 1191749a25ca889d514889b34cf6311c8014d97bf66Brian Paul 1206dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell inline bool isUndefined() const { return LatticeValue == undefined; } 1216dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell inline bool isConstant() const { 1226dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell return LatticeValue == constant || LatticeValue == forcedconstant; 12389fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul } 12489fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul inline bool isOverdefined() const { return LatticeValue == overdefined; } 12589fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul 12673b150c816c46a88e3e5d97f9b73ab0095f2bc60Brian Paul inline Constant *getConstant() const { 12773b150c816c46a88e3e5d97f9b73ab0095f2bc60Brian Paul assert(isConstant() && "Cannot get the constant of a non-constant!"); 128afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg return ConstantVal; 129b132e8da5e5f2b7da1f2141e0322e66bb0608e02Brian Paul } 130f9995b30756140724f41daf963fa06167912be7fKristian Høgsberg}; 131afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg 1321749a25ca889d514889b34cf6311c8014d97bf66Brian Paul//===----------------------------------------------------------------------===// 133afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg// 134afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg/// SCCPSolver - This class is a general purpose solver for Sparse Conditional 135afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg/// Constant Propagation. 136afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg/// 137afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtgclass SCCPSolver : public InstVisitor<SCCPSolver> { 138afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg SmallSet<BasicBlock*, 16> BBExecutable;// The basic blocks that are executable 139afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg std::map<Value*, LatticeVal> ValueState; // The state each value is in. 140afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg 141afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg /// GlobalValue - If we are tracking any values for the contents of a global 142afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg /// variable, we keep a mapping from the constant accessor to the element of 143afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg /// the global, to the currently known value. If the value becomes 144afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg /// overdefined, it's entry is simply removed from this map. 145afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg DenseMap<GlobalVariable*, LatticeVal> TrackedGlobals; 146afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg 147afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg /// TrackedFunctionRetVals - If we are tracking arguments into and the return 148afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg /// value out of a function, it will have an entry in this map, indicating 149afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg /// what the known return value for the function is. 150afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg DenseMap<Function*, LatticeVal> TrackedFunctionRetVals; 151afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg 152afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg // The reason for two worklists is that overdefined is the lowest state 153afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg // on the lattice, and moving things to overdefined as fast as possible 154afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg // makes SCCP converge much faster. 155afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg // By having a separate worklist, we accomplish this because everything 156afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg // possibly overdefined will become overdefined at the soonest possible 157afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg // point. 158afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg std::vector<Value*> OverdefinedInstWorkList; 159afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg std::vector<Value*> InstWorkList; 160afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg 161afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg 162afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg std::vector<BasicBlock*> BBWorkList; // The BasicBlock work list 163afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg 164afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg /// UsersOfOverdefinedPHIs - Keep track of any users of PHI nodes that are not 165afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg /// overdefined, despite the fact that the PHI node is overdefined. 166afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg std::multimap<PHINode*, Instruction*> UsersOfOverdefinedPHIs; 167afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg 168afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg /// KnownFeasibleEdges - Entries in this set are edges which have already had 169afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg /// PHI nodes retriggered. 170afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg typedef std::pair<BasicBlock*,BasicBlock*> Edge; 171afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg std::set<Edge> KnownFeasibleEdges; 172afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtgpublic: 173afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg 174afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg /// MarkBlockExecutable - This method can be used by clients to mark all of 175afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg /// the blocks that are known to be intrinsically live in the processed unit. 176afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg void MarkBlockExecutable(BasicBlock *BB) { 177afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg DOUT << "Marking Block Executable: " << BB->getName() << "\n"; 178afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg BBExecutable.insert(BB); // Basic block is executable! 179afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg BBWorkList.push_back(BB); // Add the block to the work list! 180afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg } 181f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul 182f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul /// TrackValueOfGlobalVariable - Clients can use this method to 183f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul /// inform the SCCPSolver that it should track loads and stores to the 184f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul /// specified global variable if it can. This is only legal to call if 185f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul /// performing Interprocedural SCCP. 186f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul void TrackValueOfGlobalVariable(GlobalVariable *GV) { 187f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul const Type *ElTy = GV->getType()->getElementType(); 188f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul if (ElTy->isFirstClassType()) { 189f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul LatticeVal &IV = TrackedGlobals[GV]; 190f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul if (!isa<UndefValue>(GV->getInitializer())) 191f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul IV.markConstant(GV->getInitializer()); 192f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul } 193f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul } 194e71654961868eac559210ced359c1af114138d8aBrian Paul 195f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul /// AddTrackedFunction - If the SCCP solver is supposed to track calls into 196f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul /// and out of the specified function (which cannot have its address taken), 197f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul /// this method must be called. 198f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul void AddTrackedFunction(Function *F) { 199f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul assert(F->hasInternalLinkage() && "Can only track internal functions!"); 2004741dbcbbc2514de370a760f4b78a17491014555Ian Romanick // Add an entry, F -> undef. 201f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul TrackedFunctionRetVals[F]; 202f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul } 2034741dbcbbc2514de370a760f4b78a17491014555Ian Romanick 2044741dbcbbc2514de370a760f4b78a17491014555Ian Romanick /// Solve - Solve for constants and executable blocks. 2054741dbcbbc2514de370a760f4b78a17491014555Ian Romanick /// 206f7e1dfeaefda8865252513bc4d880ea8640efe4dBrian Paul void Solve(); 207f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul 208f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul /// ResolvedUndefsIn - While solving the dataflow for a function, we assume 209f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul /// that branches on undef values cannot reach any of their successors. 210f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul /// However, this is not a safe assumption. After we solve dataflow, this 21189fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul /// method should be use to handle this. If this returns true, the solver 21233fa5e4bfad8005f09ad3c9fc92c40fa863935d1Ian Romanick /// should be rerun. 21333fa5e4bfad8005f09ad3c9fc92c40fa863935d1Ian Romanick bool ResolvedUndefsIn(Function &F); 21433fa5e4bfad8005f09ad3c9fc92c40fa863935d1Ian Romanick 21533fa5e4bfad8005f09ad3c9fc92c40fa863935d1Ian Romanick /// getExecutableBlocks - Once we have solved for constants, return the set of 21633fa5e4bfad8005f09ad3c9fc92c40fa863935d1Ian Romanick /// blocks that is known to be executable. 21733fa5e4bfad8005f09ad3c9fc92c40fa863935d1Ian Romanick SmallSet<BasicBlock*, 16> &getExecutableBlocks() { 21833fa5e4bfad8005f09ad3c9fc92c40fa863935d1Ian Romanick return BBExecutable; 21933fa5e4bfad8005f09ad3c9fc92c40fa863935d1Ian Romanick } 22033fa5e4bfad8005f09ad3c9fc92c40fa863935d1Ian Romanick 22133fa5e4bfad8005f09ad3c9fc92c40fa863935d1Ian Romanick /// getValueMapping - Once we have solved for constants, return the mapping of 22233fa5e4bfad8005f09ad3c9fc92c40fa863935d1Ian Romanick /// LLVM values to LatticeVals. 22333fa5e4bfad8005f09ad3c9fc92c40fa863935d1Ian Romanick std::map<Value*, LatticeVal> &getValueMapping() { 22433fa5e4bfad8005f09ad3c9fc92c40fa863935d1Ian Romanick return ValueState; 22533fa5e4bfad8005f09ad3c9fc92c40fa863935d1Ian Romanick } 22633fa5e4bfad8005f09ad3c9fc92c40fa863935d1Ian Romanick 227f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul /// getTrackedFunctionRetVals - Get the inferred return value map. 228f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul /// 229f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul const DenseMap<Function*, LatticeVal> &getTrackedFunctionRetVals() { 230f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul return TrackedFunctionRetVals; 231f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul } 23289fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul 233f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul /// getTrackedGlobals - Get and return the set of inferred initializers for 23489fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul /// global variables. 235f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul const DenseMap<GlobalVariable*, LatticeVal> &getTrackedGlobals() { 236f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul return TrackedGlobals; 237f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul } 238f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul 239f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul inline void markOverdefined(Value *V) { 240f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul markOverdefined(ValueState[V], V); 241f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul } 2428f04c12e0ad876baa7eb9ed379e2b00150b376e0Brian Paul 2438f04c12e0ad876baa7eb9ed379e2b00150b376e0Brian Paulprivate: 244f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul // markConstant - Make a value be marked as "constant". If the value 245f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul // is not already a constant, add it to the instruction work list so that 246f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul // the users of the instruction are updated later. 24740bd9d0b190e11d39350d1b08d2c2b28e3040bcaDaniel Borca // 248f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul inline void markConstant(LatticeVal &IV, Value *V, Constant *C) { 249f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul if (IV.markConstant(C)) { 250f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul DOUT << "markConstant: " << *C << ": " << *V; 251f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul InstWorkList.push_back(V); 252f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul } 253f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul } 254f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul 255f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul inline void markForcedConstant(LatticeVal &IV, Value *V, Constant *C) { 256f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul IV.markForcedConstant(C); 257663a9e1b7ef7b8384abe2f81e1a8749b942f6d3aDaniel Borca DOUT << "markForcedConstant: " << *C << ": " << *V; 258f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul InstWorkList.push_back(V); 259f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul } 260663a9e1b7ef7b8384abe2f81e1a8749b942f6d3aDaniel Borca 261f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul inline void markConstant(Value *V, Constant *C) { 262f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul markConstant(ValueState[V], V, C); 263f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul } 264f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul 26589fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul // markOverdefined - Make a value be marked as "overdefined". If the 266f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul // value is not already overdefined, add it to the overdefined instruction 267f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul // work list so that the users of the instruction are updated later. 268f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul 269f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul inline void markOverdefined(LatticeVal &IV, Value *V) { 2701749a25ca889d514889b34cf6311c8014d97bf66Brian Paul if (IV.markOverdefined()) { 271f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul DEBUG(DOUT << "markOverdefined: "; 272f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul if (Function *F = dyn_cast<Function>(V)) 273f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul DOUT << "Function '" << F->getName() << "'\n"; 274f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul else 275f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul DOUT << *V); 276f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul // Only instructions go on the work list 277f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul OverdefinedInstWorkList.push_back(V); 278f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul } 279f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul } 280f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul 281f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul inline void mergeInValue(LatticeVal &IV, Value *V, LatticeVal &MergeWithV) { 282f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul if (IV.isOverdefined() || MergeWithV.isUndefined()) 283f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul return; // Noop. 284f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul if (MergeWithV.isOverdefined()) 285f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul markOverdefined(IV, V); 286f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul else if (IV.isUndefined()) 287f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul markConstant(IV, V, MergeWithV.getConstant()); 288f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul else if (IV.getConstant() != MergeWithV.getConstant()) 289f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul markOverdefined(IV, V); 290f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul } 291f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul 2928f04c12e0ad876baa7eb9ed379e2b00150b376e0Brian Paul inline void mergeInValue(Value *V, LatticeVal &MergeWithV) { 293f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul return mergeInValue(ValueState[V], V, MergeWithV); 294afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg } 295f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul 296114152e068ec919feb0a57a1259c2ada970b9f02Roland Scheidegger 297114152e068ec919feb0a57a1259c2ada970b9f02Roland Scheidegger // getValueState - Return the LatticeVal object that corresponds to the value. 298114152e068ec919feb0a57a1259c2ada970b9f02Roland Scheidegger // This function is necessary because not all values should start out in the 299114152e068ec919feb0a57a1259c2ada970b9f02Roland Scheidegger // underdefined state... Argument's should be overdefined, and 300114152e068ec919feb0a57a1259c2ada970b9f02Roland Scheidegger // constants should be marked as constants. If a value is not known to be an 301114152e068ec919feb0a57a1259c2ada970b9f02Roland Scheidegger // Instruction object, then use this accessor to get its value from the map. 302114152e068ec919feb0a57a1259c2ada970b9f02Roland Scheidegger // 303114152e068ec919feb0a57a1259c2ada970b9f02Roland Scheidegger inline LatticeVal &getValueState(Value *V) { 304114152e068ec919feb0a57a1259c2ada970b9f02Roland Scheidegger std::map<Value*, LatticeVal>::iterator I = ValueState.find(V); 305114152e068ec919feb0a57a1259c2ada970b9f02Roland Scheidegger if (I != ValueState.end()) return I->second; // Common case, in the map 306c6a6cc191813e8343a17b028146a34f193a6ce44Roland Scheidegger 307c6a6cc191813e8343a17b028146a34f193a6ce44Roland Scheidegger if (Constant *C = dyn_cast<Constant>(V)) { 308c6a6cc191813e8343a17b028146a34f193a6ce44Roland Scheidegger if (isa<UndefValue>(V)) { 309c6a6cc191813e8343a17b028146a34f193a6ce44Roland Scheidegger // Nothing to do, remain undefined. 310c6a6cc191813e8343a17b028146a34f193a6ce44Roland Scheidegger } else { 311c6a6cc191813e8343a17b028146a34f193a6ce44Roland Scheidegger LatticeVal &LV = ValueState[C]; 312c6a6cc191813e8343a17b028146a34f193a6ce44Roland Scheidegger LV.markConstant(C); // Constants are constant 313c6a6cc191813e8343a17b028146a34f193a6ce44Roland Scheidegger return LV; 314c6a6cc191813e8343a17b028146a34f193a6ce44Roland Scheidegger } 315114152e068ec919feb0a57a1259c2ada970b9f02Roland Scheidegger } 3161ad7b99925e044f82e635f746c1ef2df77f69ac9Brian Paul // All others are underdefined by default... 3171ad7b99925e044f82e635f746c1ef2df77f69ac9Brian Paul return ValueState[V]; 3181ad7b99925e044f82e635f746c1ef2df77f69ac9Brian Paul } 3191ad7b99925e044f82e635f746c1ef2df77f69ac9Brian Paul 3201ad7b99925e044f82e635f746c1ef2df77f69ac9Brian Paul // markEdgeExecutable - Mark a basic block as executable, adding it to the BB 3211ad7b99925e044f82e635f746c1ef2df77f69ac9Brian Paul // work list if it is not already executable... 3221ad7b99925e044f82e635f746c1ef2df77f69ac9Brian Paul // 3231ad7b99925e044f82e635f746c1ef2df77f69ac9Brian Paul void markEdgeExecutable(BasicBlock *Source, BasicBlock *Dest) { 3241ad7b99925e044f82e635f746c1ef2df77f69ac9Brian Paul if (!KnownFeasibleEdges.insert(Edge(Source, Dest)).second) 3251ad7b99925e044f82e635f746c1ef2df77f69ac9Brian Paul return; // This edge is already known to be executable! 3260a4be7036864efa6b30d78e0aac449d34b812c13Brian Paul 3270a4be7036864efa6b30d78e0aac449d34b812c13Brian Paul if (BBExecutable.count(Dest)) { 3280a4be7036864efa6b30d78e0aac449d34b812c13Brian Paul DOUT << "Marking Edge Executable: " << Source->getName() 3290a4be7036864efa6b30d78e0aac449d34b812c13Brian Paul << " -> " << Dest->getName() << "\n"; 3300a4be7036864efa6b30d78e0aac449d34b812c13Brian Paul 3310a4be7036864efa6b30d78e0aac449d34b812c13Brian Paul // The destination is already executable, but we just made an edge 3320a4be7036864efa6b30d78e0aac449d34b812c13Brian Paul // feasible that wasn't before. Revisit the PHI nodes in the block 3330a4be7036864efa6b30d78e0aac449d34b812c13Brian Paul // because they have potentially new operands. 3340a4be7036864efa6b30d78e0aac449d34b812c13Brian Paul for (BasicBlock::iterator I = Dest->begin(); isa<PHINode>(I); ++I) 3350a4be7036864efa6b30d78e0aac449d34b812c13Brian Paul visitPHINode(*cast<PHINode>(I)); 3360a4be7036864efa6b30d78e0aac449d34b812c13Brian Paul 3370a4be7036864efa6b30d78e0aac449d34b812c13Brian Paul } else { 3380a4be7036864efa6b30d78e0aac449d34b812c13Brian Paul MarkBlockExecutable(Dest); 3390a4be7036864efa6b30d78e0aac449d34b812c13Brian Paul } 3400a4be7036864efa6b30d78e0aac449d34b812c13Brian Paul } 3410a4be7036864efa6b30d78e0aac449d34b812c13Brian Paul 3420a4be7036864efa6b30d78e0aac449d34b812c13Brian Paul // getFeasibleSuccessors - Return a vector of booleans to indicate which 3430a4be7036864efa6b30d78e0aac449d34b812c13Brian Paul // successors are reachable from a given terminator instruction. 3440a4be7036864efa6b30d78e0aac449d34b812c13Brian Paul // 3450a4be7036864efa6b30d78e0aac449d34b812c13Brian Paul void getFeasibleSuccessors(TerminatorInst &TI, SmallVector<bool, 16> &Succs); 3460a4be7036864efa6b30d78e0aac449d34b812c13Brian Paul 3470a4be7036864efa6b30d78e0aac449d34b812c13Brian Paul // isEdgeFeasible - Return true if the control flow edge from the 'From' basic 3480a4be7036864efa6b30d78e0aac449d34b812c13Brian Paul // block to the 'To' basic block is currently feasible... 3490a4be7036864efa6b30d78e0aac449d34b812c13Brian Paul // 350abd5627a6a034885b0b01b995c73870da1361bb0Brian Paul bool isEdgeFeasible(BasicBlock *From, BasicBlock *To); 3510a4be7036864efa6b30d78e0aac449d34b812c13Brian Paul 3520a4be7036864efa6b30d78e0aac449d34b812c13Brian Paul // OperandChangedState - This method is invoked on all of the users of an 3530a4be7036864efa6b30d78e0aac449d34b812c13Brian Paul // instruction that was just changed state somehow.... Based on this 3540a4be7036864efa6b30d78e0aac449d34b812c13Brian Paul // information, we need to update the specified user of this instruction. 355abd5627a6a034885b0b01b995c73870da1361bb0Brian Paul // 356abd5627a6a034885b0b01b995c73870da1361bb0Brian Paul void OperandChangedState(User *U) { 357abd5627a6a034885b0b01b995c73870da1361bb0Brian Paul // Only instructions use other variable values! 358abd5627a6a034885b0b01b995c73870da1361bb0Brian Paul Instruction &I = cast<Instruction>(*U); 359abd5627a6a034885b0b01b995c73870da1361bb0Brian Paul if (BBExecutable.count(I.getParent())) // Inst is executable? 360abd5627a6a034885b0b01b995c73870da1361bb0Brian Paul visit(I); 361abd5627a6a034885b0b01b995c73870da1361bb0Brian Paul } 362abd5627a6a034885b0b01b995c73870da1361bb0Brian Paul 363abd5627a6a034885b0b01b995c73870da1361bb0Brian Paulprivate: 364abd5627a6a034885b0b01b995c73870da1361bb0Brian Paul friend class InstVisitor<SCCPSolver>; 365abd5627a6a034885b0b01b995c73870da1361bb0Brian Paul 366abd5627a6a034885b0b01b995c73870da1361bb0Brian Paul // visit implementations - Something changed in this instruction... Either an 367abd5627a6a034885b0b01b995c73870da1361bb0Brian Paul // operand made a transition, or the instruction is newly executable. Change 368abd5627a6a034885b0b01b995c73870da1361bb0Brian Paul // the value type of I to reflect these changes if appropriate. 369abd5627a6a034885b0b01b995c73870da1361bb0Brian Paul // 370abd5627a6a034885b0b01b995c73870da1361bb0Brian Paul void visitPHINode(PHINode &I); 371abd5627a6a034885b0b01b995c73870da1361bb0Brian Paul 372abd5627a6a034885b0b01b995c73870da1361bb0Brian Paul // Terminators 373abd5627a6a034885b0b01b995c73870da1361bb0Brian Paul void visitReturnInst(ReturnInst &I); 374abd5627a6a034885b0b01b995c73870da1361bb0Brian Paul void visitTerminatorInst(TerminatorInst &TI); 375abd5627a6a034885b0b01b995c73870da1361bb0Brian Paul 376abd5627a6a034885b0b01b995c73870da1361bb0Brian Paul void visitCastInst(CastInst &I); 377abd5627a6a034885b0b01b995c73870da1361bb0Brian Paul void visitSelectInst(SelectInst &I); 378abd5627a6a034885b0b01b995c73870da1361bb0Brian Paul void visitBinaryOperator(Instruction &I); 379abd5627a6a034885b0b01b995c73870da1361bb0Brian Paul void visitCmpInst(CmpInst &I); 380abd5627a6a034885b0b01b995c73870da1361bb0Brian Paul void visitExtractElementInst(ExtractElementInst &I); 381abd5627a6a034885b0b01b995c73870da1361bb0Brian Paul void visitInsertElementInst(InsertElementInst &I); 382abd5627a6a034885b0b01b995c73870da1361bb0Brian Paul void visitShuffleVectorInst(ShuffleVectorInst &I); 383abd5627a6a034885b0b01b995c73870da1361bb0Brian Paul 384abd5627a6a034885b0b01b995c73870da1361bb0Brian Paul // Instructions that cannot be folded away... 385abd5627a6a034885b0b01b995c73870da1361bb0Brian Paul void visitStoreInst (Instruction &I); 386abd5627a6a034885b0b01b995c73870da1361bb0Brian Paul void visitLoadInst (LoadInst &I); 387abd5627a6a034885b0b01b995c73870da1361bb0Brian Paul void visitGetElementPtrInst(GetElementPtrInst &I); 388abd5627a6a034885b0b01b995c73870da1361bb0Brian Paul void visitCallInst (CallInst &I) { visitCallSite(CallSite::get(&I)); } 389abd5627a6a034885b0b01b995c73870da1361bb0Brian Paul void visitInvokeInst (InvokeInst &II) { 390abd5627a6a034885b0b01b995c73870da1361bb0Brian Paul visitCallSite(CallSite::get(&II)); 391abd5627a6a034885b0b01b995c73870da1361bb0Brian Paul visitTerminatorInst(II); 392abd5627a6a034885b0b01b995c73870da1361bb0Brian Paul } 393abd5627a6a034885b0b01b995c73870da1361bb0Brian Paul void visitCallSite (CallSite CS); 394abd5627a6a034885b0b01b995c73870da1361bb0Brian Paul void visitUnwindInst (TerminatorInst &I) { /*returns void*/ } 395abd5627a6a034885b0b01b995c73870da1361bb0Brian Paul void visitUnreachableInst(TerminatorInst &I) { /*returns void*/ } 396abd5627a6a034885b0b01b995c73870da1361bb0Brian Paul void visitAllocationInst(Instruction &I) { markOverdefined(&I); } 397abd5627a6a034885b0b01b995c73870da1361bb0Brian Paul void visitVANextInst (Instruction &I) { markOverdefined(&I); } 398abd5627a6a034885b0b01b995c73870da1361bb0Brian Paul void visitVAArgInst (Instruction &I) { markOverdefined(&I); } 399abd5627a6a034885b0b01b995c73870da1361bb0Brian Paul void visitFreeInst (Instruction &I) { /*returns void*/ } 400abd5627a6a034885b0b01b995c73870da1361bb0Brian Paul 401abd5627a6a034885b0b01b995c73870da1361bb0Brian Paul void visitInstruction(Instruction &I) { 4026988f65e43297ae63bbce30bf882f870b370096cBrian Paul // If a new instruction is added to LLVM that we don't handle... 4036988f65e43297ae63bbce30bf882f870b370096cBrian Paul cerr << "SCCP: Don't know how to handle: " << I; 4046988f65e43297ae63bbce30bf882f870b370096cBrian Paul markOverdefined(&I); // Just in case 4056988f65e43297ae63bbce30bf882f870b370096cBrian Paul } 4066988f65e43297ae63bbce30bf882f870b370096cBrian Paul}; 4073ebbc176f9200ac954d461758937e755220ac551Ian Romanick 4083ebbc176f9200ac954d461758937e755220ac551Ian Romanick} // end anonymous namespace 4093ebbc176f9200ac954d461758937e755220ac551Ian Romanick 4103ebbc176f9200ac954d461758937e755220ac551Ian Romanick 4113ebbc176f9200ac954d461758937e755220ac551Ian Romanick// getFeasibleSuccessors - Return a vector of booleans to indicate which 4126988f65e43297ae63bbce30bf882f870b370096cBrian Paul// successors are reachable from a given terminator instruction. 4133ebbc176f9200ac954d461758937e755220ac551Ian Romanick// 4143ebbc176f9200ac954d461758937e755220ac551Ian Romanickvoid SCCPSolver::getFeasibleSuccessors(TerminatorInst &TI, 4153ebbc176f9200ac954d461758937e755220ac551Ian Romanick SmallVector<bool, 16> &Succs) { 4166988f65e43297ae63bbce30bf882f870b370096cBrian Paul Succs.resize(TI.getNumSuccessors()); 4176988f65e43297ae63bbce30bf882f870b370096cBrian Paul if (BranchInst *BI = dyn_cast<BranchInst>(&TI)) { 4186988f65e43297ae63bbce30bf882f870b370096cBrian Paul if (BI->isUnconditional()) { 4196988f65e43297ae63bbce30bf882f870b370096cBrian Paul Succs[0] = true; 4206988f65e43297ae63bbce30bf882f870b370096cBrian Paul } else { 4216988f65e43297ae63bbce30bf882f870b370096cBrian Paul LatticeVal &BCValue = getValueState(BI->getCondition()); 4223ebbc176f9200ac954d461758937e755220ac551Ian Romanick if (BCValue.isOverdefined() || 4233ebbc176f9200ac954d461758937e755220ac551Ian Romanick (BCValue.isConstant() && !isa<ConstantInt>(BCValue.getConstant()))) { 4243ebbc176f9200ac954d461758937e755220ac551Ian Romanick // Overdefined condition variables, and branches on unfoldable constant 4253ebbc176f9200ac954d461758937e755220ac551Ian Romanick // conditions, mean the branch could go either way. 4263ebbc176f9200ac954d461758937e755220ac551Ian Romanick Succs[0] = Succs[1] = true; 4273ebbc176f9200ac954d461758937e755220ac551Ian Romanick } else if (BCValue.isConstant()) { 4283ebbc176f9200ac954d461758937e755220ac551Ian Romanick // Constant condition variables mean the branch can only go a single way 4293ebbc176f9200ac954d461758937e755220ac551Ian Romanick Succs[BCValue.getConstant() == ConstantInt::getFalse()] = true; 4303ebbc176f9200ac954d461758937e755220ac551Ian Romanick } 4316988f65e43297ae63bbce30bf882f870b370096cBrian Paul } 4323ebbc176f9200ac954d461758937e755220ac551Ian Romanick } else if (isa<InvokeInst>(&TI)) { 4333ebbc176f9200ac954d461758937e755220ac551Ian Romanick // Invoke instructions successors are always executable. 4343ebbc176f9200ac954d461758937e755220ac551Ian Romanick Succs[0] = Succs[1] = true; 4353ebbc176f9200ac954d461758937e755220ac551Ian Romanick } else if (SwitchInst *SI = dyn_cast<SwitchInst>(&TI)) { 4363ebbc176f9200ac954d461758937e755220ac551Ian Romanick LatticeVal &SCValue = getValueState(SI->getCondition()); 4376988f65e43297ae63bbce30bf882f870b370096cBrian Paul if (SCValue.isOverdefined() || // Overdefined condition? 4383ebbc176f9200ac954d461758937e755220ac551Ian Romanick (SCValue.isConstant() && !isa<ConstantInt>(SCValue.getConstant()))) { 4393ebbc176f9200ac954d461758937e755220ac551Ian Romanick // All destinations are executable! 4403ebbc176f9200ac954d461758937e755220ac551Ian Romanick Succs.assign(TI.getNumSuccessors(), true); 4416988f65e43297ae63bbce30bf882f870b370096cBrian Paul } else if (SCValue.isConstant()) { 4426988f65e43297ae63bbce30bf882f870b370096cBrian Paul Constant *CPV = SCValue.getConstant(); 4436988f65e43297ae63bbce30bf882f870b370096cBrian Paul // Make sure to skip the "default value" which isn't a value 4446988f65e43297ae63bbce30bf882f870b370096cBrian Paul for (unsigned i = 1, E = SI->getNumSuccessors(); i != E; ++i) { 4456988f65e43297ae63bbce30bf882f870b370096cBrian Paul if (SI->getSuccessorValue(i) == CPV) {// Found the right branch... 4466988f65e43297ae63bbce30bf882f870b370096cBrian Paul Succs[i] = true; 4473ebbc176f9200ac954d461758937e755220ac551Ian Romanick return; 4483ebbc176f9200ac954d461758937e755220ac551Ian Romanick } 4493ebbc176f9200ac954d461758937e755220ac551Ian Romanick } 4503ebbc176f9200ac954d461758937e755220ac551Ian Romanick 4513ebbc176f9200ac954d461758937e755220ac551Ian Romanick // Constant value not equal to any of the branches... must execute 4523ebbc176f9200ac954d461758937e755220ac551Ian Romanick // default branch then... 4533ebbc176f9200ac954d461758937e755220ac551Ian Romanick Succs[0] = true; 4546988f65e43297ae63bbce30bf882f870b370096cBrian Paul } 4556988f65e43297ae63bbce30bf882f870b370096cBrian Paul } else { 4566988f65e43297ae63bbce30bf882f870b370096cBrian Paul assert(0 && "SCCP: Don't know how to handle this terminator!"); 4576988f65e43297ae63bbce30bf882f870b370096cBrian Paul } 4586988f65e43297ae63bbce30bf882f870b370096cBrian Paul} 4596988f65e43297ae63bbce30bf882f870b370096cBrian Paul 4606988f65e43297ae63bbce30bf882f870b370096cBrian Paul 4616988f65e43297ae63bbce30bf882f870b370096cBrian Paul// isEdgeFeasible - Return true if the control flow edge from the 'From' basic 4626988f65e43297ae63bbce30bf882f870b370096cBrian Paul// block to the 'To' basic block is currently feasible... 4636988f65e43297ae63bbce30bf882f870b370096cBrian Paul// 4646988f65e43297ae63bbce30bf882f870b370096cBrian Paulbool SCCPSolver::isEdgeFeasible(BasicBlock *From, BasicBlock *To) { 4656988f65e43297ae63bbce30bf882f870b370096cBrian Paul assert(BBExecutable.count(To) && "Dest should always be alive!"); 4666988f65e43297ae63bbce30bf882f870b370096cBrian Paul 4676988f65e43297ae63bbce30bf882f870b370096cBrian Paul // Make sure the source basic block is executable!! 4686988f65e43297ae63bbce30bf882f870b370096cBrian Paul if (!BBExecutable.count(From)) return false; 4696988f65e43297ae63bbce30bf882f870b370096cBrian Paul 4706988f65e43297ae63bbce30bf882f870b370096cBrian Paul // Check to make sure this edge itself is actually feasible now... 4716988f65e43297ae63bbce30bf882f870b370096cBrian Paul TerminatorInst *TI = From->getTerminator(); 4726988f65e43297ae63bbce30bf882f870b370096cBrian Paul if (BranchInst *BI = dyn_cast<BranchInst>(TI)) { 4736988f65e43297ae63bbce30bf882f870b370096cBrian Paul if (BI->isUnconditional()) 4746988f65e43297ae63bbce30bf882f870b370096cBrian Paul return true; 4756988f65e43297ae63bbce30bf882f870b370096cBrian Paul else { 4766988f65e43297ae63bbce30bf882f870b370096cBrian Paul LatticeVal &BCValue = getValueState(BI->getCondition()); 4776988f65e43297ae63bbce30bf882f870b370096cBrian Paul if (BCValue.isOverdefined()) { 4786988f65e43297ae63bbce30bf882f870b370096cBrian Paul // Overdefined condition variables mean the branch could go either way. 4796988f65e43297ae63bbce30bf882f870b370096cBrian Paul return true; 4806988f65e43297ae63bbce30bf882f870b370096cBrian Paul } else if (BCValue.isConstant()) { 4816988f65e43297ae63bbce30bf882f870b370096cBrian Paul // Not branching on an evaluatable constant? 4826988f65e43297ae63bbce30bf882f870b370096cBrian Paul if (!isa<ConstantInt>(BCValue.getConstant())) return true; 4836988f65e43297ae63bbce30bf882f870b370096cBrian Paul 4846988f65e43297ae63bbce30bf882f870b370096cBrian Paul // Constant condition variables mean the branch can only go a single way 4856988f65e43297ae63bbce30bf882f870b370096cBrian Paul return BI->getSuccessor(BCValue.getConstant() == 4866988f65e43297ae63bbce30bf882f870b370096cBrian Paul ConstantInt::getFalse()) == To; 4876988f65e43297ae63bbce30bf882f870b370096cBrian Paul } 4886988f65e43297ae63bbce30bf882f870b370096cBrian Paul return false; 489e2a054b70cb5dace40fc1426cbf936366dc72fb9Ian Romanick } 4906988f65e43297ae63bbce30bf882f870b370096cBrian Paul } else if (isa<InvokeInst>(TI)) { 491e2a054b70cb5dace40fc1426cbf936366dc72fb9Ian Romanick // Invoke instructions successors are always executable. 492e2a054b70cb5dace40fc1426cbf936366dc72fb9Ian Romanick return true; 4936988f65e43297ae63bbce30bf882f870b370096cBrian Paul } else if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) { 494e2a054b70cb5dace40fc1426cbf936366dc72fb9Ian Romanick LatticeVal &SCValue = getValueState(SI->getCondition()); 495e2a054b70cb5dace40fc1426cbf936366dc72fb9Ian Romanick if (SCValue.isOverdefined()) { // Overdefined condition? 4966988f65e43297ae63bbce30bf882f870b370096cBrian Paul // All destinations are executable! 4976988f65e43297ae63bbce30bf882f870b370096cBrian Paul return true; 4986988f65e43297ae63bbce30bf882f870b370096cBrian Paul } else if (SCValue.isConstant()) { 4996988f65e43297ae63bbce30bf882f870b370096cBrian Paul Constant *CPV = SCValue.getConstant(); 500abd5627a6a034885b0b01b995c73870da1361bb0Brian Paul if (!isa<ConstantInt>(CPV)) 501abd5627a6a034885b0b01b995c73870da1361bb0Brian Paul return true; // not a foldable constant? 502f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul 503afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg // Make sure to skip the "default value" which isn't a value 504afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg for (unsigned i = 1, E = SI->getNumSuccessors(); i != E; ++i) 505afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg if (SI->getSuccessorValue(i) == CPV) // Found the taken branch... 5066dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell return SI->getSuccessor(i) == To; 507e93243f8b7d43695654a36334c8cc5cea140d23cBrian 508e93243f8b7d43695654a36334c8cc5cea140d23cBrian // Constant value not equal to any of the branches... must execute 509e93243f8b7d43695654a36334c8cc5cea140d23cBrian // default branch then... 510e93243f8b7d43695654a36334c8cc5cea140d23cBrian return SI->getDefaultDest() == To; 511e93243f8b7d43695654a36334c8cc5cea140d23cBrian } 512519b23b21f9cd6945fd17cdb26e7a6f531cdeec0Brian Paul return false; 513519b23b21f9cd6945fd17cdb26e7a6f531cdeec0Brian Paul } else { 514519b23b21f9cd6945fd17cdb26e7a6f531cdeec0Brian Paul cerr << "Unknown terminator instruction: " << *TI; 515519b23b21f9cd6945fd17cdb26e7a6f531cdeec0Brian Paul abort(); 516519b23b21f9cd6945fd17cdb26e7a6f531cdeec0Brian Paul } 517519b23b21f9cd6945fd17cdb26e7a6f531cdeec0Brian Paul} 518519b23b21f9cd6945fd17cdb26e7a6f531cdeec0Brian Paul 519519b23b21f9cd6945fd17cdb26e7a6f531cdeec0Brian Paul// visit Implementations - Something changed in this instruction... Either an 520519b23b21f9cd6945fd17cdb26e7a6f531cdeec0Brian Paul// operand made a transition, or the instruction is newly executable. Change 521519b23b21f9cd6945fd17cdb26e7a6f531cdeec0Brian Paul// the value type of I to reflect these changes if appropriate. This method 5226dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell// makes sure to do the following actions: 5238e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul// 5248e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul// 1. If a phi node merges two constants in, and has conflicting value coming 5256dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell// from different branches, or if the PHI node merges in an overdefined 5266dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell// value, then the PHI node becomes overdefined. 5276dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell// 2. If a phi node merges only constants in, and they all agree on value, the 5286dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell// PHI node becomes a constant value equal to that. 5296dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell// 3. If V <- x (op) y && isConstant(x) && isConstant(y) V = Constant 5306dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell// 4. If V <- x (op) y && (isOverdefined(x) || isOverdefined(y)) V = Overdefined 5318e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul// 5. If V <- MEM or V <- CALL or V <- (unknown) then V = Overdefined 532afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg// 6. If a conditional branch has a value that is constant, make the selected 5333893e638e6521b9c070e01c0b31d22754ff97a88Brian Paul// destination executable 5343893e638e6521b9c070e01c0b31d22754ff97a88Brian Paul// 7. If a conditional branch has a value that is overdefined, make all 5353893e638e6521b9c070e01c0b31d22754ff97a88Brian Paul// successors executable. 5363893e638e6521b9c070e01c0b31d22754ff97a88Brian Paul// 537fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paulvoid SCCPSolver::visitPHINode(PHINode &PN) { 5389c9a9abd7b953ec9b6cfc52c2f6981c5d38b1691Brian Paul LatticeVal &PNIV = getValueState(&PN); 5399c9a9abd7b953ec9b6cfc52c2f6981c5d38b1691Brian Paul if (PNIV.isOverdefined()) { 540fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul // There may be instructions using this PHI node that are not overdefined 541fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul // themselves. If so, make sure that they know that the PHI node operand 5429c9a9abd7b953ec9b6cfc52c2f6981c5d38b1691Brian Paul // changed. 5439c9a9abd7b953ec9b6cfc52c2f6981c5d38b1691Brian Paul std::multimap<PHINode*, Instruction*>::iterator I, E; 5449c9a9abd7b953ec9b6cfc52c2f6981c5d38b1691Brian Paul tie(I, E) = UsersOfOverdefinedPHIs.equal_range(&PN); 5459c9a9abd7b953ec9b6cfc52c2f6981c5d38b1691Brian Paul if (I != E) { 546a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul SmallVector<Instruction*, 16> Users; 547a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul for (; I != E; ++I) Users.push_back(I->second); 548fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul while (!Users.empty()) { 549fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul visit(Users.back()); 550fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul Users.pop_back(); 551a3f137094cd965d27e1b088499dd609b81a91906Brian Paul } 5526dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell } 5536dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell return; // Quick exit 554a3f137094cd965d27e1b088499dd609b81a91906Brian Paul } 555a3f137094cd965d27e1b088499dd609b81a91906Brian Paul 5566dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell // Super-extra-high-degree PHI nodes are unlikely to ever be marked constant, 5576dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell // and slow us down a lot. Just mark them overdefined. 5586dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell if (PN.getNumIncomingValues() > 64) { 55977ce6da028589efc2f3f16cece287f56fd98ce8eBrian Paul markOverdefined(PNIV, &PN); 56077ce6da028589efc2f3f16cece287f56fd98ce8eBrian Paul return; 561f9995b30756140724f41daf963fa06167912be7fKristian Høgsberg } 56277ce6da028589efc2f3f16cece287f56fd98ce8eBrian Paul 563a3f137094cd965d27e1b088499dd609b81a91906Brian Paul // Look at all of the executable operands of the PHI node. If any of them 56477ce6da028589efc2f3f16cece287f56fd98ce8eBrian Paul // are overdefined, the PHI becomes overdefined as well. If they are all 56577ce6da028589efc2f3f16cece287f56fd98ce8eBrian Paul // constant, and they agree with each other, the PHI becomes the identical 56677ce6da028589efc2f3f16cece287f56fd98ce8eBrian Paul // constant. If they are constant and don't agree, the PHI is overdefined. 56777ce6da028589efc2f3f16cece287f56fd98ce8eBrian Paul // If there are no executable operands, the PHI remains undefined. 568a3f137094cd965d27e1b088499dd609b81a91906Brian Paul // 5693e62d3a8d88b48d4ed19e00ea2bbc3d0a2b6acf7Keith Whitwell Constant *OperandVal = 0; 5704cf6718725c7cf3bfb728118a8b14f8cf206c701Brian Paul for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) { 5713e62d3a8d88b48d4ed19e00ea2bbc3d0a2b6acf7Keith Whitwell LatticeVal &IV = getValueState(PN.getIncomingValue(i)); 5721eee1bac1f6d911e6124daafc9b9291666d91cefVinson Lee if (IV.isUndefined()) continue; // Doesn't influence PHI node. 5733e62d3a8d88b48d4ed19e00ea2bbc3d0a2b6acf7Keith Whitwell 5743e62d3a8d88b48d4ed19e00ea2bbc3d0a2b6acf7Keith Whitwell if (isEdgeFeasible(PN.getIncomingBlock(i), PN.getParent())) { 5753e62d3a8d88b48d4ed19e00ea2bbc3d0a2b6acf7Keith Whitwell if (IV.isOverdefined()) { // PHI node becomes overdefined! 5763e62d3a8d88b48d4ed19e00ea2bbc3d0a2b6acf7Keith Whitwell markOverdefined(PNIV, &PN); 577f9995b30756140724f41daf963fa06167912be7fKristian Høgsberg return; 5784cf6718725c7cf3bfb728118a8b14f8cf206c701Brian Paul } 5793e62d3a8d88b48d4ed19e00ea2bbc3d0a2b6acf7Keith Whitwell 580c3c19be8e0d0b13916cc128cf3c8e839935c912aBrian Paul if (OperandVal == 0) { // Grab the first value... 581c3c19be8e0d0b13916cc128cf3c8e839935c912aBrian Paul OperandVal = IV.getConstant(); 5823e62d3a8d88b48d4ed19e00ea2bbc3d0a2b6acf7Keith Whitwell } else { // Another value is being merged in! 5833e62d3a8d88b48d4ed19e00ea2bbc3d0a2b6acf7Keith Whitwell // There is already a reachable operand. If we conflict with it, 5844cf6718725c7cf3bfb728118a8b14f8cf206c701Brian Paul // then the PHI node becomes overdefined. If we agree with it, we 5853e62d3a8d88b48d4ed19e00ea2bbc3d0a2b6acf7Keith Whitwell // can continue on. 5863e62d3a8d88b48d4ed19e00ea2bbc3d0a2b6acf7Keith Whitwell 5873e62d3a8d88b48d4ed19e00ea2bbc3d0a2b6acf7Keith Whitwell // Check to see if there are two different constants merging... 5883e62d3a8d88b48d4ed19e00ea2bbc3d0a2b6acf7Keith Whitwell if (IV.getConstant() != OperandVal) { 5893e62d3a8d88b48d4ed19e00ea2bbc3d0a2b6acf7Keith Whitwell // Yes there is. This means the PHI node is not constant. 5903e62d3a8d88b48d4ed19e00ea2bbc3d0a2b6acf7Keith Whitwell // You must be overdefined poor PHI. 5913e62d3a8d88b48d4ed19e00ea2bbc3d0a2b6acf7Keith Whitwell // 5926dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell markOverdefined(PNIV, &PN); // The PHI node now becomes overdefined 5936dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell return; // I'm done analyzing you 5941eee1bac1f6d911e6124daafc9b9291666d91cefVinson Lee } 5956dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell } 5963e62d3a8d88b48d4ed19e00ea2bbc3d0a2b6acf7Keith Whitwell } 597a3f137094cd965d27e1b088499dd609b81a91906Brian Paul } 59877ce6da028589efc2f3f16cece287f56fd98ce8eBrian Paul 5993c59febf05e6af80d63e5b9a478a11b275ac429cBrian Paul // If we exited the loop, this means that the PHI node only has constant 6003c59febf05e6af80d63e5b9a478a11b275ac429cBrian Paul // arguments that agree with each other(and OperandVal is the constant) or 60177ce6da028589efc2f3f16cece287f56fd98ce8eBrian Paul // OperandVal is null because there are no defined incoming arguments. If 6025ac93f86210eb5c2a8dee74ec19b0ecd54376863Keith Whitwell // this is the case, the PHI remains undefined. 6035ac93f86210eb5c2a8dee74ec19b0ecd54376863Keith Whitwell // 6045ac93f86210eb5c2a8dee74ec19b0ecd54376863Keith Whitwell if (OperandVal) 6055ac93f86210eb5c2a8dee74ec19b0ecd54376863Keith Whitwell markConstant(PNIV, &PN, OperandVal); // Acquire operand value 6065ac93f86210eb5c2a8dee74ec19b0ecd54376863Keith Whitwell} 6075ac93f86210eb5c2a8dee74ec19b0ecd54376863Keith Whitwell 6083e62d3a8d88b48d4ed19e00ea2bbc3d0a2b6acf7Keith Whitwellvoid SCCPSolver::visitReturnInst(ReturnInst &I) { 609b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul if (I.getNumOperands() == 0) return; // Ret void 61032f2fd1c5d6088692551c80352b7d6fa35b0cd09Kristian Høgsberg 61132f2fd1c5d6088692551c80352b7d6fa35b0cd09Kristian Høgsberg // If we are tracking the return value of this function, merge it in. 61277ce6da028589efc2f3f16cece287f56fd98ce8eBrian Paul Function *F = I.getParent()->getParent(); 61377ce6da028589efc2f3f16cece287f56fd98ce8eBrian Paul if (F->hasInternalLinkage() && !TrackedFunctionRetVals.empty()) { 61477ce6da028589efc2f3f16cece287f56fd98ce8eBrian Paul DenseMap<Function*, LatticeVal>::iterator TFRVI = 6156dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell TrackedFunctionRetVals.find(F); 6166dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell if (TFRVI != TrackedFunctionRetVals.end() && 6176dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell !TFRVI->second.isOverdefined()) { 6186dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell LatticeVal &IV = getValueState(I.getOperand(0)); 6196dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell mergeInValue(TFRVI->second, F, IV); 6206dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell } 621aea66b135eaa5a5f2bc8c652fa7a1a42cca2fe83Brian Paul } 62277ee31930a1b0cc7766939415f4f04ed6a1fa4acBrian Paul} 62377ee31930a1b0cc7766939415f4f04ed6a1fa4acBrian Paul 624aea66b135eaa5a5f2bc8c652fa7a1a42cca2fe83Brian Paul 6252465c4fa9cabe8c40e526b9e081de3b70c851455Brian Paulvoid SCCPSolver::visitTerminatorInst(TerminatorInst &TI) { 6262465c4fa9cabe8c40e526b9e081de3b70c851455Brian Paul SmallVector<bool, 16> SuccFeasible; 6272465c4fa9cabe8c40e526b9e081de3b70c851455Brian Paul getFeasibleSuccessors(TI, SuccFeasible); 6288e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul 6298e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul BasicBlock *BB = TI.getParent(); 6308e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul 63127f4484fb73ac7bf4f790ca2d3efd50b6bea25c3Brian Paul // Mark all feasible successors executable... 632bb372f1c9bc08e8b0dca983cb4ba36b2f2f039fbIan Romanick for (unsigned i = 0, e = SuccFeasible.size(); i != e; ++i) 633bb372f1c9bc08e8b0dca983cb4ba36b2f2f039fbIan Romanick if (SuccFeasible[i]) 634bb372f1c9bc08e8b0dca983cb4ba36b2f2f039fbIan Romanick markEdgeExecutable(BB, TI.getSuccessor(i)); 635aea66b135eaa5a5f2bc8c652fa7a1a42cca2fe83Brian Paul} 636aea66b135eaa5a5f2bc8c652fa7a1a42cca2fe83Brian Paul 637aea66b135eaa5a5f2bc8c652fa7a1a42cca2fe83Brian Paulvoid SCCPSolver::visitCastInst(CastInst &I) { 6386dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell Value *V = I.getOperand(0); 6393c59febf05e6af80d63e5b9a478a11b275ac429cBrian Paul LatticeVal &VState = getValueState(V); 6403c59febf05e6af80d63e5b9a478a11b275ac429cBrian Paul if (VState.isOverdefined()) // Inherit overdefinedness of operand 6416dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell markOverdefined(&I); 6426dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell else if (VState.isConstant()) // Propagate constant value 6436dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell markConstant(&I, ConstantExpr::getCast(I.getOpcode(), 6446dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell VState.getConstant(), I.getType())); 6456dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell} 6466dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell 6476dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwellvoid SCCPSolver::visitSelectInst(SelectInst &I) { 6486dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell LatticeVal &CondValue = getValueState(I.getCondition()); 64935d5301a54153930ee6fd60dff1010ce9f901397Brian Paul if (CondValue.isUndefined()) 65035d5301a54153930ee6fd60dff1010ce9f901397Brian Paul return; 6513c59febf05e6af80d63e5b9a478a11b275ac429cBrian Paul if (CondValue.isConstant()) { 6523c59febf05e6af80d63e5b9a478a11b275ac429cBrian Paul if (ConstantInt *CondCB = dyn_cast<ConstantInt>(CondValue.getConstant())){ 65335d5301a54153930ee6fd60dff1010ce9f901397Brian Paul mergeInValue(&I, getValueState(CondCB->getZExtValue() ? I.getTrueValue() 65435d5301a54153930ee6fd60dff1010ce9f901397Brian Paul : I.getFalseValue())); 65535d5301a54153930ee6fd60dff1010ce9f901397Brian Paul return; 65635d5301a54153930ee6fd60dff1010ce9f901397Brian Paul } 6579818734e0148510967ca9ee0d1aa8b196b509f02Brian Paul } 65835d5301a54153930ee6fd60dff1010ce9f901397Brian Paul 659fe469007037d9d5cdbe1677d8ff7368b276e9e7cBrian // Otherwise, the condition is overdefined or a constant we can't evaluate. 66035d5301a54153930ee6fd60dff1010ce9f901397Brian Paul // See if we can produce something better than overdefined based on the T/F 6619818734e0148510967ca9ee0d1aa8b196b509f02Brian Paul // value. 66235d5301a54153930ee6fd60dff1010ce9f901397Brian Paul LatticeVal &TVal = getValueState(I.getTrueValue()); 663fe469007037d9d5cdbe1677d8ff7368b276e9e7cBrian LatticeVal &FVal = getValueState(I.getFalseValue()); 66435d5301a54153930ee6fd60dff1010ce9f901397Brian Paul 6659818734e0148510967ca9ee0d1aa8b196b509f02Brian Paul // select ?, C, C -> C. 66635d5301a54153930ee6fd60dff1010ce9f901397Brian Paul if (TVal.isConstant() && FVal.isConstant() && 667fe469007037d9d5cdbe1677d8ff7368b276e9e7cBrian TVal.getConstant() == FVal.getConstant()) { 66835d5301a54153930ee6fd60dff1010ce9f901397Brian Paul markConstant(&I, FVal.getConstant()); 66935d5301a54153930ee6fd60dff1010ce9f901397Brian Paul return; 67035d5301a54153930ee6fd60dff1010ce9f901397Brian Paul } 67135d5301a54153930ee6fd60dff1010ce9f901397Brian Paul 67235d5301a54153930ee6fd60dff1010ce9f901397Brian Paul if (TVal.isUndefined()) { // select ?, undef, X -> X. 67335d5301a54153930ee6fd60dff1010ce9f901397Brian Paul mergeInValue(&I, FVal); 6745ff4075a6961b26042dc2d7f4adcf333439823f4Brian Paul } else if (FVal.isUndefined()) { // select ?, X, undef -> X. 675a96308c37db0bc0086a017d318bc3504aa5f0b1aKeith Whitwell mergeInValue(&I, TVal); 6769818734e0148510967ca9ee0d1aa8b196b509f02Brian Paul } else { 67735d5301a54153930ee6fd60dff1010ce9f901397Brian Paul markOverdefined(&I); 678a96308c37db0bc0086a017d318bc3504aa5f0b1aKeith Whitwell } 679fe469007037d9d5cdbe1677d8ff7368b276e9e7cBrian} 6808afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul 6818afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul// Handle BinaryOperators and Shift Instructions... 6829818734e0148510967ca9ee0d1aa8b196b509f02Brian Paulvoid SCCPSolver::visitBinaryOperator(Instruction &I) { 6838afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul LatticeVal &IV = ValueState[&I]; 6848afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul if (IV.isOverdefined()) return; 685fe469007037d9d5cdbe1677d8ff7368b276e9e7cBrian 686bb372f1c9bc08e8b0dca983cb4ba36b2f2f039fbIan Romanick LatticeVal &V1State = getValueState(I.getOperand(0)); 687bb372f1c9bc08e8b0dca983cb4ba36b2f2f039fbIan Romanick LatticeVal &V2State = getValueState(I.getOperand(1)); 6889818734e0148510967ca9ee0d1aa8b196b509f02Brian Paul 689bb372f1c9bc08e8b0dca983cb4ba36b2f2f039fbIan Romanick if (V1State.isOverdefined() || V2State.isOverdefined()) { 690bb372f1c9bc08e8b0dca983cb4ba36b2f2f039fbIan Romanick // If this is an AND or OR with 0 or -1, it doesn't matter that the other 691fe469007037d9d5cdbe1677d8ff7368b276e9e7cBrian // operand is overdefined. 692bb372f1c9bc08e8b0dca983cb4ba36b2f2f039fbIan Romanick if (I.getOpcode() == Instruction::And || I.getOpcode() == Instruction::Or) { 693bb372f1c9bc08e8b0dca983cb4ba36b2f2f039fbIan Romanick LatticeVal *NonOverdefVal = 0; 6949818734e0148510967ca9ee0d1aa8b196b509f02Brian Paul if (!V1State.isOverdefined()) { 695bb372f1c9bc08e8b0dca983cb4ba36b2f2f039fbIan Romanick NonOverdefVal = &V1State; 696bb372f1c9bc08e8b0dca983cb4ba36b2f2f039fbIan Romanick } else if (!V2State.isOverdefined()) { 697fe469007037d9d5cdbe1677d8ff7368b276e9e7cBrian NonOverdefVal = &V2State; 69835d5301a54153930ee6fd60dff1010ce9f901397Brian Paul } 69908836341788a9f9d638d9dc8328510ccd18ddeb5Brian Paul 70035d5301a54153930ee6fd60dff1010ce9f901397Brian Paul if (NonOverdefVal) { 70135d5301a54153930ee6fd60dff1010ce9f901397Brian Paul if (NonOverdefVal->isUndefined()) { 70235d5301a54153930ee6fd60dff1010ce9f901397Brian Paul // Could annihilate value. 70335d5301a54153930ee6fd60dff1010ce9f901397Brian Paul if (I.getOpcode() == Instruction::And) 70435d5301a54153930ee6fd60dff1010ce9f901397Brian Paul markConstant(IV, &I, Constant::getNullValue(I.getType())); 7056dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell else if (const VectorType *PT = dyn_cast<VectorType>(I.getType())) 706b8fdb900fb9b1c8b1e9ec88509624237307a869aBrian Paul markConstant(IV, &I, ConstantVector::getAllOnesValue(PT)); 707b8fdb900fb9b1c8b1e9ec88509624237307a869aBrian Paul else 708b8fdb900fb9b1c8b1e9ec88509624237307a869aBrian Paul markConstant(IV, &I, ConstantInt::getAllOnesValue(I.getType())); 709f9995b30756140724f41daf963fa06167912be7fKristian Høgsberg return; 710b8fdb900fb9b1c8b1e9ec88509624237307a869aBrian Paul } else { 711b8fdb900fb9b1c8b1e9ec88509624237307a869aBrian Paul if (I.getOpcode() == Instruction::And) { 712b8fdb900fb9b1c8b1e9ec88509624237307a869aBrian Paul if (NonOverdefVal->getConstant()->isNullValue()) { 713b8fdb900fb9b1c8b1e9ec88509624237307a869aBrian Paul markConstant(IV, &I, NonOverdefVal->getConstant()); 714b8fdb900fb9b1c8b1e9ec88509624237307a869aBrian Paul return; // X and 0 = 0 715b8fdb900fb9b1c8b1e9ec88509624237307a869aBrian Paul } 716b8fdb900fb9b1c8b1e9ec88509624237307a869aBrian Paul } else { 717c156eeb682d673e571c1798ff21e183ad4114feaBrian Paul if (ConstantInt *CI = 718c156eeb682d673e571c1798ff21e183ad4114feaBrian Paul dyn_cast<ConstantInt>(NonOverdefVal->getConstant())) 719c156eeb682d673e571c1798ff21e183ad4114feaBrian Paul if (CI->isAllOnesValue()) { 7206dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell markConstant(IV, &I, NonOverdefVal->getConstant()); 7216dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell return; // X or -1 = -1 7221eee1bac1f6d911e6124daafc9b9291666d91cefVinson Lee } 7236dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell } 7246dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell } 7256dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell } 726c156eeb682d673e571c1798ff21e183ad4114feaBrian Paul } 727fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul 728fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul 7293c59febf05e6af80d63e5b9a478a11b275ac429cBrian Paul // If both operands are PHI nodes, it is possible that this instruction has 7303c59febf05e6af80d63e5b9a478a11b275ac429cBrian Paul // a constant value, despite the fact that the PHI node doesn't. Check for 7315ac93f86210eb5c2a8dee74ec19b0ecd54376863Keith Whitwell // this condition now. 732fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul if (PHINode *PN1 = dyn_cast<PHINode>(I.getOperand(0))) 733c156eeb682d673e571c1798ff21e183ad4114feaBrian Paul if (PHINode *PN2 = dyn_cast<PHINode>(I.getOperand(1))) 7345ac93f86210eb5c2a8dee74ec19b0ecd54376863Keith Whitwell if (PN1->getParent() == PN2->getParent()) { 735c156eeb682d673e571c1798ff21e183ad4114feaBrian Paul // Since the two PHI nodes are in the same basic block, they must have 736c156eeb682d673e571c1798ff21e183ad4114feaBrian Paul // entries for the same predecessors. Walk the predecessor list, and 737c156eeb682d673e571c1798ff21e183ad4114feaBrian Paul // if all of the incoming values are constants, and the result of 738bb372f1c9bc08e8b0dca983cb4ba36b2f2f039fbIan Romanick // evaluating this expression with all incoming value pairs is the 739c156eeb682d673e571c1798ff21e183ad4114feaBrian Paul // same, then this expression is a constant even though the PHI node 740fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul // is not a constant! 741fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul LatticeVal Result; 742fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul for (unsigned i = 0, e = PN1->getNumIncomingValues(); i != e; ++i) { 743a3f137094cd965d27e1b088499dd609b81a91906Brian Paul LatticeVal &In1 = getValueState(PN1->getIncomingValue(i)); 744a3f137094cd965d27e1b088499dd609b81a91906Brian Paul BasicBlock *InBlock = PN1->getIncomingBlock(i); 745a3f137094cd965d27e1b088499dd609b81a91906Brian Paul LatticeVal &In2 = 746a3f137094cd965d27e1b088499dd609b81a91906Brian Paul getValueState(PN2->getIncomingValueForBlock(InBlock)); 747a3f137094cd965d27e1b088499dd609b81a91906Brian Paul 748a3f137094cd965d27e1b088499dd609b81a91906Brian Paul if (In1.isOverdefined() || In2.isOverdefined()) { 749f9995b30756140724f41daf963fa06167912be7fKristian Høgsberg Result.markOverdefined(); 750a3f137094cd965d27e1b088499dd609b81a91906Brian Paul break; // Cannot fold this operation over the PHI nodes! 751a3f137094cd965d27e1b088499dd609b81a91906Brian Paul } else if (In1.isConstant() && In2.isConstant()) { 752a3f137094cd965d27e1b088499dd609b81a91906Brian Paul Constant *V = ConstantExpr::get(I.getOpcode(), In1.getConstant(), 7535ac93f86210eb5c2a8dee74ec19b0ecd54376863Keith Whitwell In2.getConstant()); 7545ac93f86210eb5c2a8dee74ec19b0ecd54376863Keith Whitwell if (Result.isUndefined()) 7555ac93f86210eb5c2a8dee74ec19b0ecd54376863Keith Whitwell Result.markConstant(V); 7565ac93f86210eb5c2a8dee74ec19b0ecd54376863Keith Whitwell else if (Result.isConstant() && Result.getConstant() != V) { 7575ac93f86210eb5c2a8dee74ec19b0ecd54376863Keith Whitwell Result.markOverdefined(); 758a3f137094cd965d27e1b088499dd609b81a91906Brian Paul break; 759a3f137094cd965d27e1b088499dd609b81a91906Brian Paul } 760a3f137094cd965d27e1b088499dd609b81a91906Brian Paul } 761a3f137094cd965d27e1b088499dd609b81a91906Brian Paul } 762a3f137094cd965d27e1b088499dd609b81a91906Brian Paul 763a3f137094cd965d27e1b088499dd609b81a91906Brian Paul // If we found a constant value here, then we know the instruction is 7645ac93f86210eb5c2a8dee74ec19b0ecd54376863Keith Whitwell // constant despite the fact that the PHI nodes are overdefined. 765a3f137094cd965d27e1b088499dd609b81a91906Brian Paul if (Result.isConstant()) { 766a3f137094cd965d27e1b088499dd609b81a91906Brian Paul markConstant(IV, &I, Result.getConstant()); 7675ac93f86210eb5c2a8dee74ec19b0ecd54376863Keith Whitwell // Remember that this instruction is virtually using the PHI node 768a3f137094cd965d27e1b088499dd609b81a91906Brian Paul // operands. 769a3f137094cd965d27e1b088499dd609b81a91906Brian Paul UsersOfOverdefinedPHIs.insert(std::make_pair(PN1, &I)); 770a3f137094cd965d27e1b088499dd609b81a91906Brian Paul UsersOfOverdefinedPHIs.insert(std::make_pair(PN2, &I)); 771a3f137094cd965d27e1b088499dd609b81a91906Brian Paul return; 772a3f137094cd965d27e1b088499dd609b81a91906Brian Paul } else if (Result.isUndefined()) { 773a3f137094cd965d27e1b088499dd609b81a91906Brian Paul return; 774a3f137094cd965d27e1b088499dd609b81a91906Brian Paul } 775a3f137094cd965d27e1b088499dd609b81a91906Brian Paul 776a3f137094cd965d27e1b088499dd609b81a91906Brian Paul // Okay, this really is overdefined now. Since we might have 777a3f137094cd965d27e1b088499dd609b81a91906Brian Paul // speculatively thought that this was not overdefined before, and 778a3f137094cd965d27e1b088499dd609b81a91906Brian Paul // added ourselves to the UsersOfOverdefinedPHIs list for the PHIs, 779f9995b30756140724f41daf963fa06167912be7fKristian Høgsberg // make sure to clean out any entries that we put there, for 780a3f137094cd965d27e1b088499dd609b81a91906Brian Paul // efficiency. 781a3f137094cd965d27e1b088499dd609b81a91906Brian Paul std::multimap<PHINode*, Instruction*>::iterator It, E; 78280684649a6d01f0e0517b14f61cbcad6fa101929Brian Paul tie(It, E) = UsersOfOverdefinedPHIs.equal_range(PN1); 783a3f137094cd965d27e1b088499dd609b81a91906Brian Paul while (It != E) { 784a3f137094cd965d27e1b088499dd609b81a91906Brian Paul if (It->second == &I) { 785a3f137094cd965d27e1b088499dd609b81a91906Brian Paul UsersOfOverdefinedPHIs.erase(It++); 786a3f137094cd965d27e1b088499dd609b81a91906Brian Paul } else 787a3f137094cd965d27e1b088499dd609b81a91906Brian Paul ++It; 788a3f137094cd965d27e1b088499dd609b81a91906Brian Paul } 789a3f137094cd965d27e1b088499dd609b81a91906Brian Paul tie(It, E) = UsersOfOverdefinedPHIs.equal_range(PN2); 790a3f137094cd965d27e1b088499dd609b81a91906Brian Paul while (It != E) { 79180684649a6d01f0e0517b14f61cbcad6fa101929Brian Paul if (It->second == &I) { 79280684649a6d01f0e0517b14f61cbcad6fa101929Brian Paul UsersOfOverdefinedPHIs.erase(It++); 793a3f137094cd965d27e1b088499dd609b81a91906Brian Paul } else 794a3f137094cd965d27e1b088499dd609b81a91906Brian Paul ++It; 795a3f137094cd965d27e1b088499dd609b81a91906Brian Paul } 79680684649a6d01f0e0517b14f61cbcad6fa101929Brian Paul } 79780684649a6d01f0e0517b14f61cbcad6fa101929Brian Paul 798a3f137094cd965d27e1b088499dd609b81a91906Brian Paul markOverdefined(IV, &I); 799a3f137094cd965d27e1b088499dd609b81a91906Brian Paul } else if (V1State.isConstant() && V2State.isConstant()) { 800a3f137094cd965d27e1b088499dd609b81a91906Brian Paul markConstant(IV, &I, ConstantExpr::get(I.getOpcode(), V1State.getConstant(), 80180684649a6d01f0e0517b14f61cbcad6fa101929Brian Paul V2State.getConstant())); 80280684649a6d01f0e0517b14f61cbcad6fa101929Brian Paul } 803a3f137094cd965d27e1b088499dd609b81a91906Brian Paul} 804a3f137094cd965d27e1b088499dd609b81a91906Brian Paul 805a3f137094cd965d27e1b088499dd609b81a91906Brian Paul// Handle ICmpInst instruction... 80680684649a6d01f0e0517b14f61cbcad6fa101929Brian Paulvoid SCCPSolver::visitCmpInst(CmpInst &I) { 80780684649a6d01f0e0517b14f61cbcad6fa101929Brian Paul LatticeVal &IV = ValueState[&I]; 808a3f137094cd965d27e1b088499dd609b81a91906Brian Paul if (IV.isOverdefined()) return; 809a3f137094cd965d27e1b088499dd609b81a91906Brian Paul 810a3f137094cd965d27e1b088499dd609b81a91906Brian Paul LatticeVal &V1State = getValueState(I.getOperand(0)); 81180684649a6d01f0e0517b14f61cbcad6fa101929Brian Paul LatticeVal &V2State = getValueState(I.getOperand(1)); 81280684649a6d01f0e0517b14f61cbcad6fa101929Brian Paul 813bb372f1c9bc08e8b0dca983cb4ba36b2f2f039fbIan Romanick if (V1State.isOverdefined() || V2State.isOverdefined()) { 814bb372f1c9bc08e8b0dca983cb4ba36b2f2f039fbIan Romanick // If both operands are PHI nodes, it is possible that this instruction has 815bb372f1c9bc08e8b0dca983cb4ba36b2f2f039fbIan Romanick // a constant value, despite the fact that the PHI node doesn't. Check for 81680684649a6d01f0e0517b14f61cbcad6fa101929Brian Paul // this condition now. 81780684649a6d01f0e0517b14f61cbcad6fa101929Brian Paul if (PHINode *PN1 = dyn_cast<PHINode>(I.getOperand(0))) 818bb372f1c9bc08e8b0dca983cb4ba36b2f2f039fbIan Romanick if (PHINode *PN2 = dyn_cast<PHINode>(I.getOperand(1))) 819bb372f1c9bc08e8b0dca983cb4ba36b2f2f039fbIan Romanick if (PN1->getParent() == PN2->getParent()) { 820bb372f1c9bc08e8b0dca983cb4ba36b2f2f039fbIan Romanick // Since the two PHI nodes are in the same basic block, they must have 82180684649a6d01f0e0517b14f61cbcad6fa101929Brian Paul // entries for the same predecessors. Walk the predecessor list, and 82280684649a6d01f0e0517b14f61cbcad6fa101929Brian Paul // if all of the incoming values are constants, and the result of 823a3f137094cd965d27e1b088499dd609b81a91906Brian Paul // evaluating this expression with all incoming value pairs is the 824a3f137094cd965d27e1b088499dd609b81a91906Brian Paul // same, then this expression is a constant even though the PHI node 825a3f137094cd965d27e1b088499dd609b81a91906Brian Paul // is not a constant! 82680684649a6d01f0e0517b14f61cbcad6fa101929Brian Paul LatticeVal Result; 82780684649a6d01f0e0517b14f61cbcad6fa101929Brian Paul for (unsigned i = 0, e = PN1->getNumIncomingValues(); i != e; ++i) { 82880684649a6d01f0e0517b14f61cbcad6fa101929Brian Paul LatticeVal &In1 = getValueState(PN1->getIncomingValue(i)); 82980684649a6d01f0e0517b14f61cbcad6fa101929Brian Paul BasicBlock *InBlock = PN1->getIncomingBlock(i); 83080684649a6d01f0e0517b14f61cbcad6fa101929Brian Paul LatticeVal &In2 = 83180684649a6d01f0e0517b14f61cbcad6fa101929Brian Paul getValueState(PN2->getIncomingValueForBlock(InBlock)); 83280684649a6d01f0e0517b14f61cbcad6fa101929Brian Paul 83380684649a6d01f0e0517b14f61cbcad6fa101929Brian Paul if (In1.isOverdefined() || In2.isOverdefined()) { 83480684649a6d01f0e0517b14f61cbcad6fa101929Brian Paul Result.markOverdefined(); 83580684649a6d01f0e0517b14f61cbcad6fa101929Brian Paul break; // Cannot fold this operation over the PHI nodes! 83680684649a6d01f0e0517b14f61cbcad6fa101929Brian Paul } else if (In1.isConstant() && In2.isConstant()) { 83780684649a6d01f0e0517b14f61cbcad6fa101929Brian Paul Constant *V = ConstantExpr::getCompare(I.getPredicate(), 83880684649a6d01f0e0517b14f61cbcad6fa101929Brian Paul In1.getConstant(), 839a3f137094cd965d27e1b088499dd609b81a91906Brian Paul In2.getConstant()); 840a3f137094cd965d27e1b088499dd609b81a91906Brian Paul if (Result.isUndefined()) 841a3f137094cd965d27e1b088499dd609b81a91906Brian Paul Result.markConstant(V); 8426dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell else if (Result.isConstant() && Result.getConstant() != V) { 8436dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell Result.markOverdefined(); 8446dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell break; 8456dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell } 8466dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell } 8476dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell } 8486dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell 8496dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell // If we found a constant value here, then we know the instruction is 8506dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell // constant despite the fact that the PHI nodes are overdefined. 8516dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell if (Result.isConstant()) { 852ef31f60b12abc2109568fb8d9a2aaa70ec5c71ccBrian Paul markConstant(IV, &I, Result.getConstant()); 853ef31f60b12abc2109568fb8d9a2aaa70ec5c71ccBrian Paul // Remember that this instruction is virtually using the PHI node 854f9995b30756140724f41daf963fa06167912be7fKristian Høgsberg // operands. 855ef31f60b12abc2109568fb8d9a2aaa70ec5c71ccBrian Paul UsersOfOverdefinedPHIs.insert(std::make_pair(PN1, &I)); 856ef31f60b12abc2109568fb8d9a2aaa70ec5c71ccBrian Paul UsersOfOverdefinedPHIs.insert(std::make_pair(PN2, &I)); 857ef31f60b12abc2109568fb8d9a2aaa70ec5c71ccBrian Paul return; 858ef31f60b12abc2109568fb8d9a2aaa70ec5c71ccBrian Paul } else if (Result.isUndefined()) { 859ef31f60b12abc2109568fb8d9a2aaa70ec5c71ccBrian Paul return; 860ef31f60b12abc2109568fb8d9a2aaa70ec5c71ccBrian Paul } 861ef31f60b12abc2109568fb8d9a2aaa70ec5c71ccBrian Paul 862ef31f60b12abc2109568fb8d9a2aaa70ec5c71ccBrian Paul // Okay, this really is overdefined now. Since we might have 863ef31f60b12abc2109568fb8d9a2aaa70ec5c71ccBrian Paul // speculatively thought that this was not overdefined before, and 864ef31f60b12abc2109568fb8d9a2aaa70ec5c71ccBrian Paul // added ourselves to the UsersOfOverdefinedPHIs list for the PHIs, 865ef31f60b12abc2109568fb8d9a2aaa70ec5c71ccBrian Paul // make sure to clean out any entries that we put there, for 866ef31f60b12abc2109568fb8d9a2aaa70ec5c71ccBrian Paul // efficiency. 867ef31f60b12abc2109568fb8d9a2aaa70ec5c71ccBrian Paul std::multimap<PHINode*, Instruction*>::iterator It, E; 868ef31f60b12abc2109568fb8d9a2aaa70ec5c71ccBrian Paul tie(It, E) = UsersOfOverdefinedPHIs.equal_range(PN1); 869ef31f60b12abc2109568fb8d9a2aaa70ec5c71ccBrian Paul while (It != E) { 870ef31f60b12abc2109568fb8d9a2aaa70ec5c71ccBrian Paul if (It->second == &I) { 871add9f2168a5d6b15eb9955ee761246c4f4cf8458Brian Paul UsersOfOverdefinedPHIs.erase(It++); 872ef31f60b12abc2109568fb8d9a2aaa70ec5c71ccBrian Paul } else 873fe988d786c4076bfbf410b84085d8c1115baa489Brian Paul ++It; 874fe988d786c4076bfbf410b84085d8c1115baa489Brian Paul } 875ef31f60b12abc2109568fb8d9a2aaa70ec5c71ccBrian Paul tie(It, E) = UsersOfOverdefinedPHIs.equal_range(PN2); 876ef31f60b12abc2109568fb8d9a2aaa70ec5c71ccBrian Paul while (It != E) { 877fe988d786c4076bfbf410b84085d8c1115baa489Brian Paul if (It->second == &I) { 878fe988d786c4076bfbf410b84085d8c1115baa489Brian Paul UsersOfOverdefinedPHIs.erase(It++); 879fe988d786c4076bfbf410b84085d8c1115baa489Brian Paul } else 880fe988d786c4076bfbf410b84085d8c1115baa489Brian Paul ++It; 881fe988d786c4076bfbf410b84085d8c1115baa489Brian Paul } 882fe988d786c4076bfbf410b84085d8c1115baa489Brian Paul } 883fe988d786c4076bfbf410b84085d8c1115baa489Brian Paul 884ef31f60b12abc2109568fb8d9a2aaa70ec5c71ccBrian Paul markOverdefined(IV, &I); 885ef31f60b12abc2109568fb8d9a2aaa70ec5c71ccBrian Paul } else if (V1State.isConstant() && V2State.isConstant()) { 886ef31f60b12abc2109568fb8d9a2aaa70ec5c71ccBrian Paul markConstant(IV, &I, ConstantExpr::getCompare(I.getPredicate(), 887ef31f60b12abc2109568fb8d9a2aaa70ec5c71ccBrian Paul V1State.getConstant(), 888ef31f60b12abc2109568fb8d9a2aaa70ec5c71ccBrian Paul V2State.getConstant())); 889ef31f60b12abc2109568fb8d9a2aaa70ec5c71ccBrian Paul } 89083e93b6008213ad86607027e8434ecaccc8b1a2cBrian Paul} 8915e3733fadf08178fca7c9f20a0f4783f940383aaBrian Paul 89283e93b6008213ad86607027e8434ecaccc8b1a2cBrian Paulvoid SCCPSolver::visitExtractElementInst(ExtractElementInst &I) { 89383e93b6008213ad86607027e8434ecaccc8b1a2cBrian Paul // FIXME : SCCP does not handle vectors properly. 89483e93b6008213ad86607027e8434ecaccc8b1a2cBrian Paul markOverdefined(&I); 89583e93b6008213ad86607027e8434ecaccc8b1a2cBrian Paul return; 89683e93b6008213ad86607027e8434ecaccc8b1a2cBrian Paul 89783e93b6008213ad86607027e8434ecaccc8b1a2cBrian Paul#if 0 89883e93b6008213ad86607027e8434ecaccc8b1a2cBrian Paul LatticeVal &ValState = getValueState(I.getOperand(0)); 89983e93b6008213ad86607027e8434ecaccc8b1a2cBrian Paul LatticeVal &IdxState = getValueState(I.getOperand(1)); 90083e93b6008213ad86607027e8434ecaccc8b1a2cBrian Paul 90183e93b6008213ad86607027e8434ecaccc8b1a2cBrian Paul if (ValState.isOverdefined() || IdxState.isOverdefined()) 90283e93b6008213ad86607027e8434ecaccc8b1a2cBrian Paul markOverdefined(&I); 90383e93b6008213ad86607027e8434ecaccc8b1a2cBrian Paul else if(ValState.isConstant() && IdxState.isConstant()) 90483e93b6008213ad86607027e8434ecaccc8b1a2cBrian Paul markConstant(&I, ConstantExpr::getExtractElement(ValState.getConstant(), 90583e93b6008213ad86607027e8434ecaccc8b1a2cBrian Paul IdxState.getConstant())); 90683e93b6008213ad86607027e8434ecaccc8b1a2cBrian Paul#endif 90783e93b6008213ad86607027e8434ecaccc8b1a2cBrian Paul} 90883e93b6008213ad86607027e8434ecaccc8b1a2cBrian Paul 90983e93b6008213ad86607027e8434ecaccc8b1a2cBrian Paulvoid SCCPSolver::visitInsertElementInst(InsertElementInst &I) { 91083e93b6008213ad86607027e8434ecaccc8b1a2cBrian Paul // FIXME : SCCP does not handle vectors properly. 91183e93b6008213ad86607027e8434ecaccc8b1a2cBrian Paul markOverdefined(&I); 9125e3733fadf08178fca7c9f20a0f4783f940383aaBrian Paul return; 9135e3733fadf08178fca7c9f20a0f4783f940383aaBrian Paul#if 0 91483e93b6008213ad86607027e8434ecaccc8b1a2cBrian Paul LatticeVal &ValState = getValueState(I.getOperand(0)); 91583e93b6008213ad86607027e8434ecaccc8b1a2cBrian Paul LatticeVal &EltState = getValueState(I.getOperand(1)); 91683e93b6008213ad86607027e8434ecaccc8b1a2cBrian Paul LatticeVal &IdxState = getValueState(I.getOperand(2)); 9175e3733fadf08178fca7c9f20a0f4783f940383aaBrian Paul 9185e3733fadf08178fca7c9f20a0f4783f940383aaBrian Paul if (ValState.isOverdefined() || EltState.isOverdefined() || 91983e93b6008213ad86607027e8434ecaccc8b1a2cBrian Paul IdxState.isOverdefined()) 92083e93b6008213ad86607027e8434ecaccc8b1a2cBrian Paul markOverdefined(&I); 92183e93b6008213ad86607027e8434ecaccc8b1a2cBrian Paul else if(ValState.isConstant() && EltState.isConstant() && 92283e93b6008213ad86607027e8434ecaccc8b1a2cBrian Paul IdxState.isConstant()) 92383e93b6008213ad86607027e8434ecaccc8b1a2cBrian Paul markConstant(&I, ConstantExpr::getInsertElement(ValState.getConstant(), 92483e93b6008213ad86607027e8434ecaccc8b1a2cBrian Paul EltState.getConstant(), 92583e93b6008213ad86607027e8434ecaccc8b1a2cBrian Paul IdxState.getConstant())); 92683e93b6008213ad86607027e8434ecaccc8b1a2cBrian Paul else if (ValState.isUndefined() && EltState.isConstant() && 92783e93b6008213ad86607027e8434ecaccc8b1a2cBrian Paul IdxState.isConstant()) 92883e93b6008213ad86607027e8434ecaccc8b1a2cBrian Paul markConstant(&I,ConstantExpr::getInsertElement(UndefValue::get(I.getType()), 929fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul EltState.getConstant(), 93062c734f49948df7aeef55ad23a6664cbf3e11533Brian Paul IdxState.getConstant())); 931f93b3dd69e744cf1dd6b102a11cdb07c2df4a967Brian Paul#endif 932afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg} 933afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg 9348e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paulvoid SCCPSolver::visitShuffleVectorInst(ShuffleVectorInst &I) { 935afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg // FIXME : SCCP does not handle vectors properly. 9368e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul markOverdefined(&I); 9378e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul return; 938afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg#if 0 9398e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul LatticeVal &V1State = getValueState(I.getOperand(0)); 9408e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul LatticeVal &V2State = getValueState(I.getOperand(1)); 9418e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul LatticeVal &MaskState = getValueState(I.getOperand(2)); 942afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg 943a1503b00f863a48a517939a42d512f9cfe77f79cBrian Paul if (MaskState.isUndefined() || 944afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg (V1State.isUndefined() && V2State.isUndefined())) 945afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg return; // Undefined output if mask or both inputs undefined. 946afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg 947afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg if (V1State.isOverdefined() || V2State.isOverdefined() || 948afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg MaskState.isOverdefined()) { 9498e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul markOverdefined(&I); 95065d54604c387dca986c876e811362d8e8517dcacBrian Paul } else { 951afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg // A mix of constant/undef inputs. 952afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg Constant *V1 = V1State.isConstant() ? 953afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg V1State.getConstant() : UndefValue::get(I.getType()); 954afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg Constant *V2 = V2State.isConstant() ? 955afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg V2State.getConstant() : UndefValue::get(I.getType()); 956afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg Constant *Mask = MaskState.isConstant() ? 957afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg MaskState.getConstant() : UndefValue::get(I.getOperand(2)->getType()); 958afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg markConstant(&I, ConstantExpr::getShuffleVector(V1, V2, Mask)); 959afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg } 960afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg#endif 9618e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul} 9628e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul 9638e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul// Handle getelementptr instructions... if all operands are constants then we 9648e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul// can turn this into a getelementptr ConstantExpr. 9658e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul// 9668e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paulvoid SCCPSolver::visitGetElementPtrInst(GetElementPtrInst &I) { 9678e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul LatticeVal &IV = ValueState[&I]; 9688e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul if (IV.isOverdefined()) return; 9698e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul 9708e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul SmallVector<Constant*, 8> Operands; 9718e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul Operands.reserve(I.getNumOperands()); 972afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg 973afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) { 974afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg LatticeVal &State = getValueState(I.getOperand(i)); 975afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg if (State.isUndefined()) 976a1503b00f863a48a517939a42d512f9cfe77f79cBrian Paul return; // Operands are not resolved yet... 9778e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul else if (State.isOverdefined()) { 9788e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul markOverdefined(IV, &I); 979afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg return; 98062c734f49948df7aeef55ad23a6664cbf3e11533Brian Paul } 981afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg assert(State.isConstant() && "Unknown state!"); 982afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg Operands.push_back(State.getConstant()); 983afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg } 984738318bb75dea8dac4465f53850987f6062a732dBrian Paul 985f378ab825c0c74aab263e7dec30194eead22c288Brian Paul Constant *Ptr = Operands[0]; 9866dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell Operands.erase(Operands.begin()); // Erase the pointer from idx list... 9876dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell 9886dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell markConstant(IV, &I, ConstantExpr::getGetElementPtr(Ptr, &Operands[0], 9899c27278acfb786c8f2fc591eef9ed0c25135bcf0Brian Paul Operands.size())); 9909c27278acfb786c8f2fc591eef9ed0c25135bcf0Brian Paul} 991f378ab825c0c74aab263e7dec30194eead22c288Brian Paul 992f378ab825c0c74aab263e7dec30194eead22c288Brian Paulvoid SCCPSolver::visitStoreInst(Instruction &SI) { 993f378ab825c0c74aab263e7dec30194eead22c288Brian Paul if (TrackedGlobals.empty() || !isa<GlobalVariable>(SI.getOperand(1))) 9949c27278acfb786c8f2fc591eef9ed0c25135bcf0Brian Paul return; 9959c27278acfb786c8f2fc591eef9ed0c25135bcf0Brian Paul GlobalVariable *GV = cast<GlobalVariable>(SI.getOperand(1)); 996f378ab825c0c74aab263e7dec30194eead22c288Brian Paul DenseMap<GlobalVariable*, LatticeVal>::iterator I = TrackedGlobals.find(GV); 9979c27278acfb786c8f2fc591eef9ed0c25135bcf0Brian Paul if (I == TrackedGlobals.end() || I->second.isOverdefined()) return; 9989c27278acfb786c8f2fc591eef9ed0c25135bcf0Brian Paul 999a9fc8ba756dd25a07dc19058fe60f65bda82a055Brian Paul // Get the value we are storing into the global. 1000a9fc8ba756dd25a07dc19058fe60f65bda82a055Brian Paul LatticeVal &PtrVal = getValueState(SI.getOperand(0)); 10019c27278acfb786c8f2fc591eef9ed0c25135bcf0Brian Paul 10029c27278acfb786c8f2fc591eef9ed0c25135bcf0Brian Paul mergeInValue(I->second, GV, PtrVal); 10039c27278acfb786c8f2fc591eef9ed0c25135bcf0Brian Paul if (I->second.isOverdefined()) 10049c27278acfb786c8f2fc591eef9ed0c25135bcf0Brian Paul TrackedGlobals.erase(I); // No need to keep tracking this! 1005681b8c9d1ba06c8c82e687a5ced369b72e6b1eb9Brian Paul} 1006b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul 100732f2fd1c5d6088692551c80352b7d6fa35b0cd09Kristian Høgsberg 1008b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul// Handle load instructions. If the operand is a constant pointer to a constant 1009b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul// global, we can replace the load with the loaded constant value! 10109c27278acfb786c8f2fc591eef9ed0c25135bcf0Brian Paulvoid SCCPSolver::visitLoadInst(LoadInst &I) { 10119c27278acfb786c8f2fc591eef9ed0c25135bcf0Brian Paul LatticeVal &IV = ValueState[&I]; 10129c27278acfb786c8f2fc591eef9ed0c25135bcf0Brian Paul if (IV.isOverdefined()) return; 10139c27278acfb786c8f2fc591eef9ed0c25135bcf0Brian Paul 10149c27278acfb786c8f2fc591eef9ed0c25135bcf0Brian Paul LatticeVal &PtrVal = getValueState(I.getOperand(0)); 10159c27278acfb786c8f2fc591eef9ed0c25135bcf0Brian Paul if (PtrVal.isUndefined()) return; // The pointer is not resolved yet! 10169c27278acfb786c8f2fc591eef9ed0c25135bcf0Brian Paul if (PtrVal.isConstant() && !I.isVolatile()) { 10171f7c914ad0beea8a29c1a171c7cd1a12f2efe0faBrian Paul Value *Ptr = PtrVal.getConstant(); 10184f295cee73bae1f687efe2dc062522b40d90b1e4Brian Paul // TODO: Consider a target hook for valid address spaces for this xform. 10194f295cee73bae1f687efe2dc062522b40d90b1e4Brian Paul if (isa<ConstantPointerNull>(Ptr) && 10209c27278acfb786c8f2fc591eef9ed0c25135bcf0Brian Paul cast<PointerType>(Ptr->getType())->getAddressSpace() == 0) { 10219c27278acfb786c8f2fc591eef9ed0c25135bcf0Brian Paul // load null -> null 10229c27278acfb786c8f2fc591eef9ed0c25135bcf0Brian Paul markConstant(IV, &I, Constant::getNullValue(I.getType())); 10236dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell return; 10246628bc9cff74a6d524165e809f73eabc85ba34b5Brian Paul } 10256dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell 10266dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell // Transform load (constant global) into the value loaded. 1027b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Ptr)) { 10286dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell if (GV->isConstant()) { 10296dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell if (!GV->isDeclaration()) { 10306dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell markConstant(IV, &I, GV->getInitializer()); 10316dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell return; 10326dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell } 10336dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell } else if (!TrackedGlobals.empty()) { 103483e93b6008213ad86607027e8434ecaccc8b1a2cBrian Paul // If we are tracking this global, merge in the known value for it. 10356dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell DenseMap<GlobalVariable*, LatticeVal>::iterator It = 10366dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell TrackedGlobals.find(GV); 1037887e2cf01a99f7fe1b7c94320b7bdbbf0d6ad2f8Brian Paul if (It != TrackedGlobals.end()) { 10386628bc9cff74a6d524165e809f73eabc85ba34b5Brian Paul mergeInValue(IV, &I, It->second); 10393893e638e6521b9c070e01c0b31d22754ff97a88Brian Paul return; 1040f9995b30756140724f41daf963fa06167912be7fKristian Høgsberg } 10413893e638e6521b9c070e01c0b31d22754ff97a88Brian Paul } 10423893e638e6521b9c070e01c0b31d22754ff97a88Brian Paul } 104383e93b6008213ad86607027e8434ecaccc8b1a2cBrian Paul 104483e93b6008213ad86607027e8434ecaccc8b1a2cBrian Paul // Transform load (constantexpr_GEP global, 0, ...) into the value loaded. 10456628bc9cff74a6d524165e809f73eabc85ba34b5Brian Paul if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr)) 104683e93b6008213ad86607027e8434ecaccc8b1a2cBrian Paul if (CE->getOpcode() == Instruction::GetElementPtr) 1047b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul if (GlobalVariable *GV = dyn_cast<GlobalVariable>(CE->getOperand(0))) 10486628bc9cff74a6d524165e809f73eabc85ba34b5Brian Paul if (GV->isConstant() && !GV->isDeclaration()) 10498bc00c2047f69d8cd37d4fd70256850445b0fa1dBrian Paul if (Constant *V = 10508bc00c2047f69d8cd37d4fd70256850445b0fa1dBrian Paul ConstantFoldLoadThroughGEPConstantExpr(GV->getInitializer(), CE)) { 10518bc00c2047f69d8cd37d4fd70256850445b0fa1dBrian Paul markConstant(IV, &I, V); 1052b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul return; 1053a9fc8ba756dd25a07dc19058fe60f65bda82a055Brian Paul } 1054a9fc8ba756dd25a07dc19058fe60f65bda82a055Brian Paul } 1055a9fc8ba756dd25a07dc19058fe60f65bda82a055Brian Paul 10566628bc9cff74a6d524165e809f73eabc85ba34b5Brian Paul // Otherwise we cannot say for certain what value this load will produce. 10576628bc9cff74a6d524165e809f73eabc85ba34b5Brian Paul // Bail out. 10586628bc9cff74a6d524165e809f73eabc85ba34b5Brian Paul markOverdefined(IV, &I); 10596628bc9cff74a6d524165e809f73eabc85ba34b5Brian Paul} 1060c813b545ab4726fc5030f123ec6255224d64ad82Brian 10614d0b7618cb3ada3b13e9e9b650ace34f5131e318Brian Paulvoid SCCPSolver::visitCallSite(CallSite CS) { 10624d0b7618cb3ada3b13e9e9b650ace34f5131e318Brian Paul Function *F = CS.getCalledFunction(); 1063c813b545ab4726fc5030f123ec6255224d64ad82Brian 1064c813b545ab4726fc5030f123ec6255224d64ad82Brian // If we are tracking this function, we must make sure to bind arguments as 1065c813b545ab4726fc5030f123ec6255224d64ad82Brian // appropriate. 10666628bc9cff74a6d524165e809f73eabc85ba34b5Brian Paul DenseMap<Function*, LatticeVal>::iterator TFRVI =TrackedFunctionRetVals.end(); 1067c813b545ab4726fc5030f123ec6255224d64ad82Brian if (F && F->hasInternalLinkage()) 1068c813b545ab4726fc5030f123ec6255224d64ad82Brian TFRVI = TrackedFunctionRetVals.find(F); 1069c813b545ab4726fc5030f123ec6255224d64ad82Brian 10704d0b7618cb3ada3b13e9e9b650ace34f5131e318Brian Paul if (TFRVI != TrackedFunctionRetVals.end()) { 1071c813b545ab4726fc5030f123ec6255224d64ad82Brian // If this is the first call to the function hit, mark its entry block 1072c813b545ab4726fc5030f123ec6255224d64ad82Brian // executable. 1073c813b545ab4726fc5030f123ec6255224d64ad82Brian if (!BBExecutable.count(F->begin())) 1074c813b545ab4726fc5030f123ec6255224d64ad82Brian MarkBlockExecutable(F->begin()); 10756628bc9cff74a6d524165e809f73eabc85ba34b5Brian Paul 1076c813b545ab4726fc5030f123ec6255224d64ad82Brian CallSite::arg_iterator CAI = CS.arg_begin(); 1077c813b545ab4726fc5030f123ec6255224d64ad82Brian for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end(); 1078c813b545ab4726fc5030f123ec6255224d64ad82Brian AI != E; ++AI, ++CAI) { 10794d0b7618cb3ada3b13e9e9b650ace34f5131e318Brian Paul LatticeVal &IV = ValueState[AI]; 1080c813b545ab4726fc5030f123ec6255224d64ad82Brian if (!IV.isOverdefined()) 1081c813b545ab4726fc5030f123ec6255224d64ad82Brian mergeInValue(IV, AI, getValueState(*CAI)); 10826628bc9cff74a6d524165e809f73eabc85ba34b5Brian Paul } 1083c813b545ab4726fc5030f123ec6255224d64ad82Brian } 1084be1b8e5d6c6692010a3ec117035d9b218929e2b3Brian Instruction *I = CS.getInstruction(); 1085be1b8e5d6c6692010a3ec117035d9b218929e2b3Brian if (I->getType() == Type::VoidTy) return; 1086be1b8e5d6c6692010a3ec117035d9b218929e2b3Brian 1087887e2cf01a99f7fe1b7c94320b7bdbbf0d6ad2f8Brian Paul LatticeVal &IV = ValueState[I]; 1088887e2cf01a99f7fe1b7c94320b7bdbbf0d6ad2f8Brian Paul if (IV.isOverdefined()) return; 1089887e2cf01a99f7fe1b7c94320b7bdbbf0d6ad2f8Brian Paul 1090887e2cf01a99f7fe1b7c94320b7bdbbf0d6ad2f8Brian Paul // Propagate the return value of the function to the value of the instruction. 1091b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul if (TFRVI != TrackedFunctionRetVals.end()) { 1092b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul mergeInValue(IV, I, TFRVI->second); 1093b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul return; 1094b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul } 1095b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul 1096b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul if (F == 0 || !F->isDeclaration() || !canConstantFoldCallTo(F)) { 1097a120778c72324bc56c63cd0f1873c6f2772228eaMichel Dänzer markOverdefined(IV, I); 109832f2fd1c5d6088692551c80352b7d6fa35b0cd09Kristian Høgsberg return; 109932f2fd1c5d6088692551c80352b7d6fa35b0cd09Kristian Høgsberg } 1100b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul 1101b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul SmallVector<Constant*, 8> Operands; 1102b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul Operands.reserve(I->getNumOperands()-1); 1103b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul 11048afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul for (CallSite::arg_iterator AI = CS.arg_begin(), E = CS.arg_end(); 11058afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul AI != E; ++AI) { 11068afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul LatticeVal &State = getValueState(*AI); 11078afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul if (State.isUndefined()) 11088afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul return; // Operands are not resolved yet... 11098afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul else if (State.isOverdefined()) { 11108afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul markOverdefined(IV, I); 11118afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul return; 11128afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul } 11138afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul assert(State.isConstant() && "Unknown state!"); 11148afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul Operands.push_back(State.getConstant()); 11158afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul } 11161a2bb37264b4448d33f2948fe1702c9dc936395dBrian Paul 111783e93b6008213ad86607027e8434ecaccc8b1a2cBrian Paul if (Constant *C = ConstantFoldCall(F, &Operands[0], Operands.size())) 111883e93b6008213ad86607027e8434ecaccc8b1a2cBrian Paul markConstant(IV, I, C); 111983e93b6008213ad86607027e8434ecaccc8b1a2cBrian Paul else 112083e93b6008213ad86607027e8434ecaccc8b1a2cBrian Paul markOverdefined(IV, I); 112183e93b6008213ad86607027e8434ecaccc8b1a2cBrian Paul} 11226628bc9cff74a6d524165e809f73eabc85ba34b5Brian Paul 11236628bc9cff74a6d524165e809f73eabc85ba34b5Brian Paul 11246628bc9cff74a6d524165e809f73eabc85ba34b5Brian Paulvoid SCCPSolver::Solve() { 1125738318bb75dea8dac4465f53850987f6062a732dBrian Paul // Process the work lists until they are empty! 112642b6b067ac68ac1309d0570613bea4a88f745559Chia-I Wu while (!BBWorkList.empty() || !InstWorkList.empty() || 112742b6b067ac68ac1309d0570613bea4a88f745559Chia-I Wu !OverdefinedInstWorkList.empty()) { 112842b6b067ac68ac1309d0570613bea4a88f745559Chia-I Wu // Process the instruction work list... 112942b6b067ac68ac1309d0570613bea4a88f745559Chia-I Wu while (!OverdefinedInstWorkList.empty()) { 113042b6b067ac68ac1309d0570613bea4a88f745559Chia-I Wu Value *I = OverdefinedInstWorkList.back(); 113142b6b067ac68ac1309d0570613bea4a88f745559Chia-I Wu OverdefinedInstWorkList.pop_back(); 113242b6b067ac68ac1309d0570613bea4a88f745559Chia-I Wu 113342b6b067ac68ac1309d0570613bea4a88f745559Chia-I Wu DOUT << "\nPopped off OI-WL: " << *I; 113442b6b067ac68ac1309d0570613bea4a88f745559Chia-I Wu 11353c59febf05e6af80d63e5b9a478a11b275ac429cBrian Paul // "I" got into the work list because it either made the transition from 11363c59febf05e6af80d63e5b9a478a11b275ac429cBrian Paul // bottom to constant 113742b6b067ac68ac1309d0570613bea4a88f745559Chia-I Wu // 113842b6b067ac68ac1309d0570613bea4a88f745559Chia-I Wu // Anything on this worklist that is overdefined need not be visited 113942b6b067ac68ac1309d0570613bea4a88f745559Chia-I Wu // since all of its users will have already been marked as overdefined 114042b6b067ac68ac1309d0570613bea4a88f745559Chia-I Wu // Update all of the users of this instruction's value... 114142b6b067ac68ac1309d0570613bea4a88f745559Chia-I Wu // 114242b6b067ac68ac1309d0570613bea4a88f745559Chia-I Wu for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); 114342b6b067ac68ac1309d0570613bea4a88f745559Chia-I Wu UI != E; ++UI) 1144738318bb75dea8dac4465f53850987f6062a732dBrian Paul OperandChangedState(*UI); 1145738318bb75dea8dac4465f53850987f6062a732dBrian Paul } 1146738318bb75dea8dac4465f53850987f6062a732dBrian Paul // Process the instruction work list... 1147738318bb75dea8dac4465f53850987f6062a732dBrian Paul while (!InstWorkList.empty()) { 1148738318bb75dea8dac4465f53850987f6062a732dBrian Paul Value *I = InstWorkList.back(); 1149738318bb75dea8dac4465f53850987f6062a732dBrian Paul InstWorkList.pop_back(); 1150aa328291c5b015e74ebfd9c5cdb39227265b3000Brian 1151aa328291c5b015e74ebfd9c5cdb39227265b3000Brian DOUT << "\nPopped off I-WL: " << *I; 1152aa328291c5b015e74ebfd9c5cdb39227265b3000Brian 1153aa328291c5b015e74ebfd9c5cdb39227265b3000Brian // "I" got into the work list because it either made the transition from 1154738318bb75dea8dac4465f53850987f6062a732dBrian Paul // bottom to constant 1155738318bb75dea8dac4465f53850987f6062a732dBrian Paul // 1156738318bb75dea8dac4465f53850987f6062a732dBrian Paul // Anything on this worklist that is overdefined need not be visited 1157738318bb75dea8dac4465f53850987f6062a732dBrian Paul // since all of its users will have already been marked as overdefined. 1158738318bb75dea8dac4465f53850987f6062a732dBrian Paul // Update all of the users of this instruction's value... 1159738318bb75dea8dac4465f53850987f6062a732dBrian Paul // 1160738318bb75dea8dac4465f53850987f6062a732dBrian Paul if (!getValueState(I).isOverdefined()) 1161738318bb75dea8dac4465f53850987f6062a732dBrian Paul for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); 1162738318bb75dea8dac4465f53850987f6062a732dBrian Paul UI != E; ++UI) 1163738318bb75dea8dac4465f53850987f6062a732dBrian Paul OperandChangedState(*UI); 1164738318bb75dea8dac4465f53850987f6062a732dBrian Paul } 1165738318bb75dea8dac4465f53850987f6062a732dBrian Paul 1166738318bb75dea8dac4465f53850987f6062a732dBrian Paul // Process the basic block work list... 1167738318bb75dea8dac4465f53850987f6062a732dBrian Paul while (!BBWorkList.empty()) { 1168f9995b30756140724f41daf963fa06167912be7fKristian Høgsberg BasicBlock *BB = BBWorkList.back(); 1169738318bb75dea8dac4465f53850987f6062a732dBrian Paul BBWorkList.pop_back(); 1170738318bb75dea8dac4465f53850987f6062a732dBrian Paul 1171738318bb75dea8dac4465f53850987f6062a732dBrian Paul DOUT << "\nPopped off BBWL: " << *BB; 1172738318bb75dea8dac4465f53850987f6062a732dBrian Paul 11739c27278acfb786c8f2fc591eef9ed0c25135bcf0Brian Paul // Notify all instructions in this basic block that they are newly 1174738318bb75dea8dac4465f53850987f6062a732dBrian Paul // executable. 1175738318bb75dea8dac4465f53850987f6062a732dBrian Paul visit(BB); 1176738318bb75dea8dac4465f53850987f6062a732dBrian Paul } 1177738318bb75dea8dac4465f53850987f6062a732dBrian Paul } 1178738318bb75dea8dac4465f53850987f6062a732dBrian Paul} 1179738318bb75dea8dac4465f53850987f6062a732dBrian Paul 1180738318bb75dea8dac4465f53850987f6062a732dBrian Paul/// ResolvedUndefsIn - While solving the dataflow for a function, we assume 1181738318bb75dea8dac4465f53850987f6062a732dBrian Paul/// that branches on undef values cannot reach any of their successors. 1182887e2cf01a99f7fe1b7c94320b7bdbbf0d6ad2f8Brian Paul/// However, this is not a safe assumption. After we solve dataflow, this 1183be1b8e5d6c6692010a3ec117035d9b218929e2b3Brian/// method should be use to handle this. If this returns true, the solver 1184738318bb75dea8dac4465f53850987f6062a732dBrian Paul/// should be rerun. 1185738318bb75dea8dac4465f53850987f6062a732dBrian Paul/// 1186738318bb75dea8dac4465f53850987f6062a732dBrian Paul/// This method handles this by finding an unresolved branch and marking it one 1187738318bb75dea8dac4465f53850987f6062a732dBrian Paul/// of the edges from the block as being feasible, even though the condition 1188738318bb75dea8dac4465f53850987f6062a732dBrian Paul/// doesn't say it would otherwise be. This allows SCCP to find the rest of the 1189738318bb75dea8dac4465f53850987f6062a732dBrian Paul/// CFG and only slightly pessimizes the analysis results (by marking one, 1190738318bb75dea8dac4465f53850987f6062a732dBrian Paul/// potentially infeasible, edge feasible). This cannot usefully modify the 1191738318bb75dea8dac4465f53850987f6062a732dBrian Paul/// constraints on the condition of the branch, as that would impact other users 1192887e2cf01a99f7fe1b7c94320b7bdbbf0d6ad2f8Brian Paul/// of the value. 1193be1b8e5d6c6692010a3ec117035d9b218929e2b3Brian/// 1194738318bb75dea8dac4465f53850987f6062a732dBrian Paul/// This scan also checks for values that use undefs, whose results are actually 1195887e2cf01a99f7fe1b7c94320b7bdbbf0d6ad2f8Brian Paul/// defined. For example, 'zext i8 undef to i32' should produce all zeros 1196be1b8e5d6c6692010a3ec117035d9b218929e2b3Brian/// conservatively, as "(zext i8 X -> i32) & 0xFF00" must always return zero, 1197738318bb75dea8dac4465f53850987f6062a732dBrian Paul/// even if X isn't defined. 1198738318bb75dea8dac4465f53850987f6062a732dBrian Paulbool SCCPSolver::ResolvedUndefsIn(Function &F) { 1199738318bb75dea8dac4465f53850987f6062a732dBrian Paul for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) { 1200738318bb75dea8dac4465f53850987f6062a732dBrian Paul if (!BBExecutable.count(BB)) 1201738318bb75dea8dac4465f53850987f6062a732dBrian Paul continue; 1202738318bb75dea8dac4465f53850987f6062a732dBrian Paul 1203738318bb75dea8dac4465f53850987f6062a732dBrian Paul for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) { 1204738318bb75dea8dac4465f53850987f6062a732dBrian Paul // Look for instructions which produce undef values. 1205887e2cf01a99f7fe1b7c94320b7bdbbf0d6ad2f8Brian Paul if (I->getType() == Type::VoidTy) continue; 1206be1b8e5d6c6692010a3ec117035d9b218929e2b3Brian 1207738318bb75dea8dac4465f53850987f6062a732dBrian Paul LatticeVal &LV = getValueState(I); 1208887e2cf01a99f7fe1b7c94320b7bdbbf0d6ad2f8Brian Paul if (!LV.isUndefined()) continue; 1209be1b8e5d6c6692010a3ec117035d9b218929e2b3Brian 1210738318bb75dea8dac4465f53850987f6062a732dBrian Paul // Get the lattice values of the first two operands for use below. 1211887e2cf01a99f7fe1b7c94320b7bdbbf0d6ad2f8Brian Paul LatticeVal &Op0LV = getValueState(I->getOperand(0)); 1212be1b8e5d6c6692010a3ec117035d9b218929e2b3Brian LatticeVal Op1LV; 1213738318bb75dea8dac4465f53850987f6062a732dBrian Paul if (I->getNumOperands() == 2) { 1214738318bb75dea8dac4465f53850987f6062a732dBrian Paul // If this is a two-operand instruction, and if both operands are 1215738318bb75dea8dac4465f53850987f6062a732dBrian Paul // undefs, the result stays undef. 1216738318bb75dea8dac4465f53850987f6062a732dBrian Paul Op1LV = getValueState(I->getOperand(1)); 1217738318bb75dea8dac4465f53850987f6062a732dBrian Paul if (Op0LV.isUndefined() && Op1LV.isUndefined()) 1218738318bb75dea8dac4465f53850987f6062a732dBrian Paul continue; 1219aa328291c5b015e74ebfd9c5cdb39227265b3000Brian } 1220aa328291c5b015e74ebfd9c5cdb39227265b3000Brian 1221738318bb75dea8dac4465f53850987f6062a732dBrian Paul // If this is an instructions whose result is defined even if the input is 1222738318bb75dea8dac4465f53850987f6062a732dBrian Paul // not fully defined, propagate the information. 1223738318bb75dea8dac4465f53850987f6062a732dBrian Paul const Type *ITy = I->getType(); 1224738318bb75dea8dac4465f53850987f6062a732dBrian Paul switch (I->getOpcode()) { 1225738318bb75dea8dac4465f53850987f6062a732dBrian Paul default: break; // Leave the instruction as an undef. 1226738318bb75dea8dac4465f53850987f6062a732dBrian Paul case Instruction::ZExt: 1227738318bb75dea8dac4465f53850987f6062a732dBrian Paul // After a zero extend, we know the top part is zero. SExt doesn't have 1228738318bb75dea8dac4465f53850987f6062a732dBrian Paul // to be handled here, because we don't know whether the top part is 1's 1229887e2cf01a99f7fe1b7c94320b7bdbbf0d6ad2f8Brian Paul // or 0's. 1230be1b8e5d6c6692010a3ec117035d9b218929e2b3Brian assert(Op0LV.isUndefined()); 1231738318bb75dea8dac4465f53850987f6062a732dBrian Paul markForcedConstant(LV, I, Constant::getNullValue(ITy)); 1232887e2cf01a99f7fe1b7c94320b7bdbbf0d6ad2f8Brian Paul return true; 1233be1b8e5d6c6692010a3ec117035d9b218929e2b3Brian case Instruction::Mul: 1234738318bb75dea8dac4465f53850987f6062a732dBrian Paul case Instruction::And: 1235738318bb75dea8dac4465f53850987f6062a732dBrian Paul // undef * X -> 0. X could be zero. 1236738318bb75dea8dac4465f53850987f6062a732dBrian Paul // undef & X -> 0. X could be zero. 1237738318bb75dea8dac4465f53850987f6062a732dBrian Paul markForcedConstant(LV, I, Constant::getNullValue(ITy)); 1238738318bb75dea8dac4465f53850987f6062a732dBrian Paul return true; 1239bb372f1c9bc08e8b0dca983cb4ba36b2f2f039fbIan Romanick 1240bb372f1c9bc08e8b0dca983cb4ba36b2f2f039fbIan Romanick case Instruction::Or: 1241bb372f1c9bc08e8b0dca983cb4ba36b2f2f039fbIan Romanick // undef | X -> -1. X could be -1. 1242bb372f1c9bc08e8b0dca983cb4ba36b2f2f039fbIan Romanick if (const VectorType *PTy = dyn_cast<VectorType>(ITy)) 1243be1b8e5d6c6692010a3ec117035d9b218929e2b3Brian markForcedConstant(LV, I, ConstantVector::getAllOnesValue(PTy)); 1244bb372f1c9bc08e8b0dca983cb4ba36b2f2f039fbIan Romanick else 1245bb372f1c9bc08e8b0dca983cb4ba36b2f2f039fbIan Romanick markForcedConstant(LV, I, ConstantInt::getAllOnesValue(ITy)); 1246bb372f1c9bc08e8b0dca983cb4ba36b2f2f039fbIan Romanick return true; 1247bb372f1c9bc08e8b0dca983cb4ba36b2f2f039fbIan Romanick 1248bb372f1c9bc08e8b0dca983cb4ba36b2f2f039fbIan Romanick case Instruction::SDiv: 1249bb372f1c9bc08e8b0dca983cb4ba36b2f2f039fbIan Romanick case Instruction::UDiv: 1250bb372f1c9bc08e8b0dca983cb4ba36b2f2f039fbIan Romanick case Instruction::SRem: 1251bb372f1c9bc08e8b0dca983cb4ba36b2f2f039fbIan Romanick case Instruction::URem: 1252bb372f1c9bc08e8b0dca983cb4ba36b2f2f039fbIan Romanick // X / undef -> undef. No change. 1253bb372f1c9bc08e8b0dca983cb4ba36b2f2f039fbIan Romanick // X % undef -> undef. No change. 1254bb372f1c9bc08e8b0dca983cb4ba36b2f2f039fbIan Romanick if (Op1LV.isUndefined()) break; 1255bb372f1c9bc08e8b0dca983cb4ba36b2f2f039fbIan Romanick 1256bb372f1c9bc08e8b0dca983cb4ba36b2f2f039fbIan Romanick // undef / X -> 0. X could be maxint. 1257be1b8e5d6c6692010a3ec117035d9b218929e2b3Brian // undef % X -> 0. X could be 1. 1258bb372f1c9bc08e8b0dca983cb4ba36b2f2f039fbIan Romanick markForcedConstant(LV, I, Constant::getNullValue(ITy)); 1259bb372f1c9bc08e8b0dca983cb4ba36b2f2f039fbIan Romanick return true; 1260be1b8e5d6c6692010a3ec117035d9b218929e2b3Brian 1261bb372f1c9bc08e8b0dca983cb4ba36b2f2f039fbIan Romanick case Instruction::AShr: 1262bb372f1c9bc08e8b0dca983cb4ba36b2f2f039fbIan Romanick // undef >>s X -> undef. No change. 1263bb372f1c9bc08e8b0dca983cb4ba36b2f2f039fbIan Romanick if (Op0LV.isUndefined()) break; 1264bb372f1c9bc08e8b0dca983cb4ba36b2f2f039fbIan Romanick 1265bb372f1c9bc08e8b0dca983cb4ba36b2f2f039fbIan Romanick // X >>s undef -> X. X could be 0, X could have the high-bit known set. 1266bb372f1c9bc08e8b0dca983cb4ba36b2f2f039fbIan Romanick if (Op0LV.isConstant()) 1267bb372f1c9bc08e8b0dca983cb4ba36b2f2f039fbIan Romanick markForcedConstant(LV, I, Op0LV.getConstant()); 1268bb372f1c9bc08e8b0dca983cb4ba36b2f2f039fbIan Romanick else 1269738318bb75dea8dac4465f53850987f6062a732dBrian Paul markOverdefined(LV, I); 1270738318bb75dea8dac4465f53850987f6062a732dBrian Paul return true; 1271738318bb75dea8dac4465f53850987f6062a732dBrian Paul case Instruction::LShr: 1272738318bb75dea8dac4465f53850987f6062a732dBrian Paul case Instruction::Shl: 1273738318bb75dea8dac4465f53850987f6062a732dBrian Paul // undef >> X -> undef. No change. 1274738318bb75dea8dac4465f53850987f6062a732dBrian Paul // undef << X -> undef. No change. 1275738318bb75dea8dac4465f53850987f6062a732dBrian Paul if (Op0LV.isUndefined()) break; 1276738318bb75dea8dac4465f53850987f6062a732dBrian Paul 1277064cd7c78c3108012f5d15206c70470f7b500259Ian Romanick // X >> undef -> 0. X could be 0. 1278064cd7c78c3108012f5d15206c70470f7b500259Ian Romanick // X << undef -> 0. X could be 0. 1279064cd7c78c3108012f5d15206c70470f7b500259Ian Romanick markForcedConstant(LV, I, Constant::getNullValue(ITy)); 1280f9995b30756140724f41daf963fa06167912be7fKristian Høgsberg return true; 1281064cd7c78c3108012f5d15206c70470f7b500259Ian Romanick case Instruction::Select: 1282646afcc34045cd482e79ded241aac23082e65f6cBrian Paul // undef ? X : Y -> X or Y. There could be commonality between X/Y. 1283646afcc34045cd482e79ded241aac23082e65f6cBrian Paul if (Op0LV.isUndefined()) { 1284646afcc34045cd482e79ded241aac23082e65f6cBrian Paul if (!Op1LV.isConstant()) // Pick the constant one if there is any. 1285646afcc34045cd482e79ded241aac23082e65f6cBrian Paul Op1LV = getValueState(I->getOperand(2)); 1286646afcc34045cd482e79ded241aac23082e65f6cBrian Paul } else if (Op1LV.isUndefined()) { 1287646afcc34045cd482e79ded241aac23082e65f6cBrian Paul // c ? undef : undef -> undef. No change. 1288646afcc34045cd482e79ded241aac23082e65f6cBrian Paul Op1LV = getValueState(I->getOperand(2)); 1289646afcc34045cd482e79ded241aac23082e65f6cBrian Paul if (Op1LV.isUndefined()) 1290646afcc34045cd482e79ded241aac23082e65f6cBrian Paul break; 1291646afcc34045cd482e79ded241aac23082e65f6cBrian Paul // Otherwise, c ? undef : x -> x. 1292646afcc34045cd482e79ded241aac23082e65f6cBrian Paul } else { 1293646afcc34045cd482e79ded241aac23082e65f6cBrian Paul // Leave Op1LV as Operand(1)'s LatticeValue. 1294646afcc34045cd482e79ded241aac23082e65f6cBrian Paul } 1295646afcc34045cd482e79ded241aac23082e65f6cBrian Paul 1296646afcc34045cd482e79ded241aac23082e65f6cBrian Paul if (Op1LV.isConstant()) 1297646afcc34045cd482e79ded241aac23082e65f6cBrian Paul markForcedConstant(LV, I, Op1LV.getConstant()); 1298646afcc34045cd482e79ded241aac23082e65f6cBrian Paul else 1299646afcc34045cd482e79ded241aac23082e65f6cBrian Paul markOverdefined(LV, I); 1300064cd7c78c3108012f5d15206c70470f7b500259Ian Romanick return true; 1301064cd7c78c3108012f5d15206c70470f7b500259Ian Romanick } 1302064cd7c78c3108012f5d15206c70470f7b500259Ian Romanick } 1303064cd7c78c3108012f5d15206c70470f7b500259Ian Romanick 13046dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell TerminatorInst *TI = BB->getTerminator(); 13056dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell if (BranchInst *BI = dyn_cast<BranchInst>(TI)) { 13066dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell if (!BI->isConditional()) continue; 130785288ad722aa0ad378004c523a0e1a4984e15316Brian Paul if (!getValueState(BI->getCondition()).isUndefined()) 13086dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell continue; 13096dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell } else if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) { 13106dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell if (!getValueState(SI->getCondition()).isUndefined()) 13116dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell continue; 13126dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell } else { 13136dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell continue; 13146dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell } 13156dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell 13166dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell // If the edge to the second successor isn't thought to be feasible yet, 13176dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell // mark it so now. We pick the second one so that this goes to some 13186dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell // enumerated value in a switch instead of going to the default destination. 13196dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell if (KnownFeasibleEdges.count(Edge(BB, TI->getSuccessor(1)))) 13206dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell continue; 13213c59febf05e6af80d63e5b9a478a11b275ac429cBrian Paul 13223c59febf05e6af80d63e5b9a478a11b275ac429cBrian Paul // Otherwise, it isn't already thought to be feasible. Mark it as such now 1323afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg // and return. This will make other blocks reachable, which will allow new 1324c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul // values to be discovered and existing ones to be moved in the lattice. 132585288ad722aa0ad378004c523a0e1a4984e15316Brian Paul markEdgeExecutable(BB, TI->getSuccessor(1)); 132685288ad722aa0ad378004c523a0e1a4984e15316Brian Paul 1327c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul // This must be a conditional branch of switch on undef. At this point, 1328c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul // force the old terminator to branch to the first successor. This is 1329c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul // required because we are now influencing the dataflow of the function with 1330c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul // the assumption that this edge is taken. If we leave the branch condition 1331afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg // as undef, then further analysis could think the undef went another way 133277ee31930a1b0cc7766939415f4f04ed6a1fa4acBrian Paul // leading to an inconsistent set of conclusions. 1333e2e4b60c7d9fc3618c0f9d7496c9ce3d5eee3ab5Ian Romanick if (BranchInst *BI = dyn_cast<BranchInst>(TI)) { 1334714c36c1202cd49c58cf6462afd391fd059b96c2Daniel Borca BI->setCondition(ConstantInt::getFalse()); 1335e2e4b60c7d9fc3618c0f9d7496c9ce3d5eee3ab5Ian Romanick } else { 1336afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg SwitchInst *SI = cast<SwitchInst>(TI); 1337738318bb75dea8dac4465f53850987f6062a732dBrian Paul SI->setCondition(SI->getCaseValue(1)); 1338738318bb75dea8dac4465f53850987f6062a732dBrian Paul } 1339738318bb75dea8dac4465f53850987f6062a732dBrian Paul 1340738318bb75dea8dac4465f53850987f6062a732dBrian Paul return true; 1341738318bb75dea8dac4465f53850987f6062a732dBrian Paul } 13428afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul 1343738318bb75dea8dac4465f53850987f6062a732dBrian Paul return false; 1344738318bb75dea8dac4465f53850987f6062a732dBrian Paul} 1345738318bb75dea8dac4465f53850987f6062a732dBrian Paul 1346738318bb75dea8dac4465f53850987f6062a732dBrian Paul 1347738318bb75dea8dac4465f53850987f6062a732dBrian Paulnamespace { 1348738318bb75dea8dac4465f53850987f6062a732dBrian Paul //===--------------------------------------------------------------------===// 1349738318bb75dea8dac4465f53850987f6062a732dBrian Paul // 1350738318bb75dea8dac4465f53850987f6062a732dBrian Paul /// SCCP Class - This class uses the SCCPSolver to implement a per-function 1351738318bb75dea8dac4465f53850987f6062a732dBrian Paul /// Sparse Conditional Constant Propagator. 1352738318bb75dea8dac4465f53850987f6062a732dBrian Paul /// 1353738318bb75dea8dac4465f53850987f6062a732dBrian Paul struct VISIBILITY_HIDDEN SCCP : public FunctionPass { 1354738318bb75dea8dac4465f53850987f6062a732dBrian Paul static char ID; // Pass identification, replacement for typeid 1355738318bb75dea8dac4465f53850987f6062a732dBrian Paul SCCP() : FunctionPass((intptr_t)&ID) {} 1356738318bb75dea8dac4465f53850987f6062a732dBrian Paul 1357973da83f6237b5af4a9ee77f32fdfa5c04ecabc8Brian Paul // runOnFunction - Run the Sparse Conditional Constant Propagation 1358f6e76fe9b5c4c57ac6dc81143b4474ebfee879d2Brian Paul // algorithm, and return true if the function was modified. 1359f6e76fe9b5c4c57ac6dc81143b4474ebfee879d2Brian Paul // 1360973da83f6237b5af4a9ee77f32fdfa5c04ecabc8Brian Paul bool runOnFunction(Function &F); 1361f6e76fe9b5c4c57ac6dc81143b4474ebfee879d2Brian Paul 1362f6e76fe9b5c4c57ac6dc81143b4474ebfee879d2Brian Paul virtual void getAnalysisUsage(AnalysisUsage &AU) const { 1363f6e76fe9b5c4c57ac6dc81143b4474ebfee879d2Brian Paul AU.setPreservesCFG(); 1364f6e76fe9b5c4c57ac6dc81143b4474ebfee879d2Brian Paul } 1365738318bb75dea8dac4465f53850987f6062a732dBrian Paul }; 1366738318bb75dea8dac4465f53850987f6062a732dBrian Paul 1367738318bb75dea8dac4465f53850987f6062a732dBrian Paul char SCCP::ID = 0; 1368738318bb75dea8dac4465f53850987f6062a732dBrian Paul RegisterPass<SCCP> X("sccp", "Sparse Conditional Constant Propagation"); 1369738318bb75dea8dac4465f53850987f6062a732dBrian Paul} // end anonymous namespace 1370e2e4b60c7d9fc3618c0f9d7496c9ce3d5eee3ab5Ian Romanick 1371e2e4b60c7d9fc3618c0f9d7496c9ce3d5eee3ab5Ian Romanick 13727c4c82fc6d5301e27643868517aeb23fcb5c40e6Brian// createSCCPPass - This is the public interface to this file... 13738afe7de8deaf3c9613fd68b344de8c52b02b1879Brian PaulFunctionPass *llvm::createSCCPPass() { 13748afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul return new SCCP(); 137508836341788a9f9d638d9dc8328510ccd18ddeb5Brian Paul} 1376afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg 1377afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg 1378afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg// runOnFunction() - Run the Sparse Conditional Constant Propagation algorithm, 1379afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg// and return true if the function was modified. 1380e2e4b60c7d9fc3618c0f9d7496c9ce3d5eee3ab5Ian Romanick// 1381738318bb75dea8dac4465f53850987f6062a732dBrian Paulbool SCCP::runOnFunction(Function &F) { 1382e2e4b60c7d9fc3618c0f9d7496c9ce3d5eee3ab5Ian Romanick DOUT << "SCCP on function '" << F.getName() << "'\n"; 1383738318bb75dea8dac4465f53850987f6062a732dBrian Paul SCCPSolver Solver; 1384738318bb75dea8dac4465f53850987f6062a732dBrian Paul 1385738318bb75dea8dac4465f53850987f6062a732dBrian Paul // Mark the first block of the function as being executable. 1386738318bb75dea8dac4465f53850987f6062a732dBrian Paul Solver.MarkBlockExecutable(F.begin()); 13878afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul 13888afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul // Mark all arguments to the function as being overdefined. 13898afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul for (Function::arg_iterator AI = F.arg_begin(), E = F.arg_end(); AI != E;++AI) 13908afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul Solver.markOverdefined(AI); 1391e2e4b60c7d9fc3618c0f9d7496c9ce3d5eee3ab5Ian Romanick 1392e2e4b60c7d9fc3618c0f9d7496c9ce3d5eee3ab5Ian Romanick // Solve for constants. 13938afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul bool ResolvedUndefs = true; 1394738318bb75dea8dac4465f53850987f6062a732dBrian Paul while (ResolvedUndefs) { 1395738318bb75dea8dac4465f53850987f6062a732dBrian Paul Solver.Solve(); 13968afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul DOUT << "RESOLVING UNDEFs\n"; 13978afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul ResolvedUndefs = Solver.ResolvedUndefsIn(F); 13988afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul } 13998afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul 1400e2e4b60c7d9fc3618c0f9d7496c9ce3d5eee3ab5Ian Romanick bool MadeChanges = false; 14018afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul 1402bb372f1c9bc08e8b0dca983cb4ba36b2f2f039fbIan Romanick // If we decided that there are basic blocks that are dead in this function, 1403bb372f1c9bc08e8b0dca983cb4ba36b2f2f039fbIan Romanick // delete their contents now. Note that we cannot actually delete the blocks, 1404bb372f1c9bc08e8b0dca983cb4ba36b2f2f039fbIan Romanick // as we cannot modify the CFG of the function. 1405bb372f1c9bc08e8b0dca983cb4ba36b2f2f039fbIan Romanick // 14068afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul SmallSet<BasicBlock*, 16> &ExecutableBBs = Solver.getExecutableBlocks(); 14078afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul SmallVector<Instruction*, 32> Insts; 14088afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul std::map<Value*, LatticeVal> &Values = Solver.getValueMapping(); 14098afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul 1410afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) 1411afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg if (!ExecutableBBs.count(BB)) { 1412738318bb75dea8dac4465f53850987f6062a732dBrian Paul DOUT << " BasicBlock Dead:" << *BB; 1413e2e4b60c7d9fc3618c0f9d7496c9ce3d5eee3ab5Ian Romanick ++NumDeadBlocks; 14148afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul 1415bb372f1c9bc08e8b0dca983cb4ba36b2f2f039fbIan Romanick // Delete the instructions backwards, as it has a reduced likelihood of 1416bb372f1c9bc08e8b0dca983cb4ba36b2f2f039fbIan Romanick // having to update as many def-use and use-def chains. 1417bb372f1c9bc08e8b0dca983cb4ba36b2f2f039fbIan Romanick for (BasicBlock::iterator I = BB->begin(), E = BB->getTerminator(); 1418bb372f1c9bc08e8b0dca983cb4ba36b2f2f039fbIan Romanick I != E; ++I) 14198afe7de8deaf3c9613fd68b344de8c52b02b1879Brian Paul Insts.push_back(I); 142008836341788a9f9d638d9dc8328510ccd18ddeb5Brian Paul while (!Insts.empty()) { 1421afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg Instruction *I = Insts.back(); 1422afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg Insts.pop_back(); 1423afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg if (!I->use_empty()) 1424afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg I->replaceAllUsesWith(UndefValue::get(I->getType())); 142508836341788a9f9d638d9dc8328510ccd18ddeb5Brian Paul BB->getInstList().erase(I); 1426afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg MadeChanges = true; 1427afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg ++NumInstRemoved; 1428afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg } 1429e2e4b60c7d9fc3618c0f9d7496c9ce3d5eee3ab5Ian Romanick } else { 1430e2e4b60c7d9fc3618c0f9d7496c9ce3d5eee3ab5Ian Romanick // Iterate over all of the instructions in a function, replacing them with 1431e2e4b60c7d9fc3618c0f9d7496c9ce3d5eee3ab5Ian Romanick // constants if we have found them to be of constant values. 1432e2e4b60c7d9fc3618c0f9d7496c9ce3d5eee3ab5Ian Romanick // 1433738318bb75dea8dac4465f53850987f6062a732dBrian Paul for (BasicBlock::iterator BI = BB->begin(), E = BB->end(); BI != E; ) { 1434c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul Instruction *Inst = BI++; 14354e9676fb13f60ecdbc247b120031f18cd3febcb0Brian Paul if (Inst->getType() != Type::VoidTy) { 1436738318bb75dea8dac4465f53850987f6062a732dBrian Paul LatticeVal &IV = Values[Inst]; 1437738318bb75dea8dac4465f53850987f6062a732dBrian Paul if ((IV.isConstant() || IV.isUndefined()) && 1438c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul !isa<TerminatorInst>(Inst)) { 1439afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg Constant *Const = IV.isConstant() 1440afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg ? IV.getConstant() : UndefValue::get(Inst->getType()); 1441afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg DOUT << " Constant: " << *Const << " = " << *Inst; 1442738318bb75dea8dac4465f53850987f6062a732dBrian Paul 144389fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul // Replaces all of the uses of a variable with uses of the constant. 1444c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul Inst->replaceAllUsesWith(Const); 14454e9676fb13f60ecdbc247b120031f18cd3febcb0Brian Paul 1446a71edc9455ef81a8dd5ec284e88061a585e63580Brian Paul // Delete the instruction. 1447a71edc9455ef81a8dd5ec284e88061a585e63580Brian Paul BB->getInstList().erase(Inst); 1448c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul 1449afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg // Hey, we just changed something! 1450afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg MadeChanges = true; 1451afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg ++NumInstRemoved; 1452738318bb75dea8dac4465f53850987f6062a732dBrian Paul } 1453f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul } 1454326b981d3fafbf0cc253d2b224f0c9ad307038a3Brian Paul } 1455326b981d3fafbf0cc253d2b224f0c9ad307038a3Brian Paul } 1456326b981d3fafbf0cc253d2b224f0c9ad307038a3Brian Paul 1457326b981d3fafbf0cc253d2b224f0c9ad307038a3Brian Paul return MadeChanges; 145838f28665bf9fb5b2464738ca5074848ec2777ae1Gareth Hughes} 145938f28665bf9fb5b2464738ca5074848ec2777ae1Gareth Hughes 1460326b981d3fafbf0cc253d2b224f0c9ad307038a3Brian Paulnamespace { 1461326b981d3fafbf0cc253d2b224f0c9ad307038a3Brian Paul //===--------------------------------------------------------------------===// 1462326b981d3fafbf0cc253d2b224f0c9ad307038a3Brian Paul // 14638a8919a7ddd2348c4a4cbcbab2358c49e47e2ea5Brian Paul /// IPSCCP Class - This class implements interprocedural Sparse Conditional 14648a8919a7ddd2348c4a4cbcbab2358c49e47e2ea5Brian Paul /// Constant Propagation. 1465c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul /// 146638f28665bf9fb5b2464738ca5074848ec2777ae1Gareth Hughes struct VISIBILITY_HIDDEN IPSCCP : public ModulePass { 1467afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg static char ID; 1468afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg IPSCCP() : ModulePass((intptr_t)&ID) {} 1469601df9c742939c1f77de489561fe3e1d02f49618Brian Paul bool runOnModule(Module &M); 147022e442544bc451f114288f07cf9cc1584ca357a1Brian Paul }; 1471ba2a55ccd61d9fa5565640faefb64fd6fb0e70abBrian Paul 147222e442544bc451f114288f07cf9cc1584ca357a1Brian Paul char IPSCCP::ID = 0; 1473ba2a55ccd61d9fa5565640faefb64fd6fb0e70abBrian Paul RegisterPass<IPSCCP> 1474ba2a55ccd61d9fa5565640faefb64fd6fb0e70abBrian Paul Y("ipsccp", "Interprocedural Sparse Conditional Constant Propagation"); 1475ba2a55ccd61d9fa5565640faefb64fd6fb0e70abBrian Paul} // end anonymous namespace 1476ba2a55ccd61d9fa5565640faefb64fd6fb0e70abBrian Paul 1477ba2a55ccd61d9fa5565640faefb64fd6fb0e70abBrian Paul// createIPSCCPPass - This is the public interface to this file... 1478601df9c742939c1f77de489561fe3e1d02f49618Brian PaulModulePass *llvm::createIPSCCPPass() { 1479601df9c742939c1f77de489561fe3e1d02f49618Brian Paul return new IPSCCP(); 14808a8919a7ddd2348c4a4cbcbab2358c49e47e2ea5Brian Paul} 14818a8919a7ddd2348c4a4cbcbab2358c49e47e2ea5Brian Paul 1482601df9c742939c1f77de489561fe3e1d02f49618Brian Paul 1483601df9c742939c1f77de489561fe3e1d02f49618Brian Paulstatic bool AddressIsTaken(GlobalValue *GV) { 1484601df9c742939c1f77de489561fe3e1d02f49618Brian Paul // Delete any dead constantexpr klingons. 1485601df9c742939c1f77de489561fe3e1d02f49618Brian Paul GV->removeDeadConstantUsers(); 1486601df9c742939c1f77de489561fe3e1d02f49618Brian Paul 1487c5b995066020191982b2315fc45d05e068eee761Brian Paul for (Value::use_iterator UI = GV->use_begin(), E = GV->use_end(); 1488601df9c742939c1f77de489561fe3e1d02f49618Brian Paul UI != E; ++UI) 1489601df9c742939c1f77de489561fe3e1d02f49618Brian Paul if (StoreInst *SI = dyn_cast<StoreInst>(*UI)) { 1490c5b995066020191982b2315fc45d05e068eee761Brian Paul if (SI->getOperand(0) == GV || SI->isVolatile()) 1491fc1be4a99425d09103bba9e06026f31f2b0142d2Vinson Lee return true; // Storing addr of GV. 14922236a301c35d29a8e0775d2b62499d8843607ee1Brian Paul } else if (isa<InvokeInst>(*UI) || isa<CallInst>(*UI)) { 1493c5dde53f4e42612518cd927bb58f08c0e22db17aVinson Lee // Make sure we are calling the function, not passing the address. 1494c5b995066020191982b2315fc45d05e068eee761Brian Paul CallSite CS = CallSite::get(cast<Instruction>(*UI)); 1495c5b995066020191982b2315fc45d05e068eee761Brian Paul for (CallSite::arg_iterator AI = CS.arg_begin(), 1496d8419c730e73c3be2eadfb0bee176ab06885766aBrian Paul E = CS.arg_end(); AI != E; ++AI) 1497d8419c730e73c3be2eadfb0bee176ab06885766aBrian Paul if (*AI == GV) 1498d8419c730e73c3be2eadfb0bee176ab06885766aBrian Paul return true; 1499d8419c730e73c3be2eadfb0bee176ab06885766aBrian Paul } else if (LoadInst *LI = dyn_cast<LoadInst>(*UI)) { 1500d8419c730e73c3be2eadfb0bee176ab06885766aBrian Paul if (LI->isVolatile()) 1501d8419c730e73c3be2eadfb0bee176ab06885766aBrian Paul return true; 1502d8419c730e73c3be2eadfb0bee176ab06885766aBrian Paul } else { 1503d8419c730e73c3be2eadfb0bee176ab06885766aBrian Paul return true; 1504d8419c730e73c3be2eadfb0bee176ab06885766aBrian Paul } 1505d8419c730e73c3be2eadfb0bee176ab06885766aBrian Paul return false; 1506d8419c730e73c3be2eadfb0bee176ab06885766aBrian Paul} 1507fc1be4a99425d09103bba9e06026f31f2b0142d2Vinson Lee 15082236a301c35d29a8e0775d2b62499d8843607ee1Brian Paulbool IPSCCP::runOnModule(Module &M) { 1509fc1be4a99425d09103bba9e06026f31f2b0142d2Vinson Lee SCCPSolver Solver; 1510c5dde53f4e42612518cd927bb58f08c0e22db17aVinson Lee 1511d8419c730e73c3be2eadfb0bee176ab06885766aBrian Paul // Loop over all functions, marking arguments to those with their addresses 1512d8419c730e73c3be2eadfb0bee176ab06885766aBrian Paul // taken or that are external as overdefined. 1513d8419c730e73c3be2eadfb0bee176ab06885766aBrian Paul // 1514c5b995066020191982b2315fc45d05e068eee761Brian Paul for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) 1515c5b995066020191982b2315fc45d05e068eee761Brian Paul if (!F->hasInternalLinkage() || AddressIsTaken(F)) { 1516601df9c742939c1f77de489561fe3e1d02f49618Brian Paul if (!F->isDeclaration()) 1517601df9c742939c1f77de489561fe3e1d02f49618Brian Paul Solver.MarkBlockExecutable(F->begin()); 15182b04dd9d2cba6ec3506e78016e64cffce6e8abf7Brian Paul for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end(); 1519601df9c742939c1f77de489561fe3e1d02f49618Brian Paul AI != E; ++AI) 1520601df9c742939c1f77de489561fe3e1d02f49618Brian Paul Solver.markOverdefined(AI); 1521601df9c742939c1f77de489561fe3e1d02f49618Brian Paul } else { 15222b04dd9d2cba6ec3506e78016e64cffce6e8abf7Brian Paul Solver.AddTrackedFunction(F); 15232b04dd9d2cba6ec3506e78016e64cffce6e8abf7Brian Paul } 15242b04dd9d2cba6ec3506e78016e64cffce6e8abf7Brian Paul 1525601df9c742939c1f77de489561fe3e1d02f49618Brian Paul // Loop over global variables. We inform the solver about any internal global 1526601df9c742939c1f77de489561fe3e1d02f49618Brian Paul // variables that do not have their 'addresses taken'. If they don't have 1527601df9c742939c1f77de489561fe3e1d02f49618Brian Paul // their addresses taken, we can propagate constants through them. 1528601df9c742939c1f77de489561fe3e1d02f49618Brian Paul for (Module::global_iterator G = M.global_begin(), E = M.global_end(); 1529601df9c742939c1f77de489561fe3e1d02f49618Brian Paul G != E; ++G) 1530601df9c742939c1f77de489561fe3e1d02f49618Brian Paul if (!G->isConstant() && G->hasInternalLinkage() && !AddressIsTaken(G)) 1531601df9c742939c1f77de489561fe3e1d02f49618Brian Paul Solver.TrackValueOfGlobalVariable(G); 1532601df9c742939c1f77de489561fe3e1d02f49618Brian Paul 1533c34feadd1c2fa5c62022c1f48ee675b25a985ac6Brian Paul // Solve for constants. 1534064cd7c78c3108012f5d15206c70470f7b500259Ian Romanick bool ResolvedUndefs = true; 1535064cd7c78c3108012f5d15206c70470f7b500259Ian Romanick while (ResolvedUndefs) { 15362236a301c35d29a8e0775d2b62499d8843607ee1Brian Paul Solver.Solve(); 1537064cd7c78c3108012f5d15206c70470f7b500259Ian Romanick 153889fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul DOUT << "RESOLVING UNDEFS\n"; 153989fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul ResolvedUndefs = false; 154089fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) 154189fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul ResolvedUndefs |= Solver.ResolvedUndefsIn(*F); 15428978e45e50118d0e3660fdd9cd58b8790c0a6bb8Vinson Lee } 154389fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul 154489fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul bool MadeChanges = false; 154589fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul 154689fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul // Iterate over all of the instructions in the module, replacing them with 154789fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul // constants if we have found them to be of constant values. 1548326b981d3fafbf0cc253d2b224f0c9ad307038a3Brian Paul // 1549326b981d3fafbf0cc253d2b224f0c9ad307038a3Brian Paul SmallSet<BasicBlock*, 16> &ExecutableBBs = Solver.getExecutableBlocks(); 1550326b981d3fafbf0cc253d2b224f0c9ad307038a3Brian Paul SmallVector<Instruction*, 32> Insts; 1551326b981d3fafbf0cc253d2b224f0c9ad307038a3Brian Paul SmallVector<BasicBlock*, 32> BlocksToErase; 1552326b981d3fafbf0cc253d2b224f0c9ad307038a3Brian Paul std::map<Value*, LatticeVal> &Values = Solver.getValueMapping(); 1553326b981d3fafbf0cc253d2b224f0c9ad307038a3Brian Paul 1554326b981d3fafbf0cc253d2b224f0c9ad307038a3Brian Paul for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) { 1555326b981d3fafbf0cc253d2b224f0c9ad307038a3Brian Paul for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end(); 1556326b981d3fafbf0cc253d2b224f0c9ad307038a3Brian Paul AI != E; ++AI) 1557326b981d3fafbf0cc253d2b224f0c9ad307038a3Brian Paul if (!AI->use_empty()) { 1558326b981d3fafbf0cc253d2b224f0c9ad307038a3Brian Paul LatticeVal &IV = Values[AI]; 1559326b981d3fafbf0cc253d2b224f0c9ad307038a3Brian Paul if (IV.isConstant() || IV.isUndefined()) { 1560afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg Constant *CST = IV.isConstant() ? 1561afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg IV.getConstant() : UndefValue::get(AI->getType()); 1562afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg DOUT << "*** Arg " << *AI << " = " << *CST <<"\n"; 1563afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg 1564afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg // Replaces all of the uses of a variable with uses of the 15656dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell // constant. 1566c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul AI->replaceAllUsesWith(CST); 15676dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell ++IPNumArgsElimed; 15686dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell } 15696dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell } 15706dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell 15716dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) 15726dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell if (!ExecutableBBs.count(BB)) { 15736dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell DOUT << " BasicBlock Dead:" << *BB; 15746dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell ++IPNumDeadBlocks; 15756dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell 15766dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell // Delete the instructions backwards, as it has a reduced likelihood of 15776dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell // having to update as many def-use and use-def chains. 15786dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell TerminatorInst *TI = BB->getTerminator(); 15796dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell for (BasicBlock::iterator I = BB->begin(), E = TI; I != E; ++I) 15806dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell Insts.push_back(I); 15816dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell 15826dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell while (!Insts.empty()) { 15836dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell Instruction *I = Insts.back(); 15843c59febf05e6af80d63e5b9a478a11b275ac429cBrian Paul Insts.pop_back(); 15853c59febf05e6af80d63e5b9a478a11b275ac429cBrian Paul if (!I->use_empty()) 1586afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg I->replaceAllUsesWith(UndefValue::get(I->getType())); 1587c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul BB->getInstList().erase(I); 1588f9995b30756140724f41daf963fa06167912be7fKristian Høgsberg MadeChanges = true; 1589c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul ++IPNumInstRemoved; 1590c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul } 1591c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul 1592c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) { 1593afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg BasicBlock *Succ = TI->getSuccessor(i); 1594738318bb75dea8dac4465f53850987f6062a732dBrian Paul if (!Succ->empty() && isa<PHINode>(Succ->begin())) 1595c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul TI->getSuccessor(i)->removePredecessor(BB); 1596738318bb75dea8dac4465f53850987f6062a732dBrian Paul } 159708836341788a9f9d638d9dc8328510ccd18ddeb5Brian Paul if (!TI->use_empty()) 1598c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul TI->replaceAllUsesWith(UndefValue::get(TI->getType())); 1599afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg BB->getInstList().erase(TI); 1600c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul 1601c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul if (&*BB != &F->front()) 1602738318bb75dea8dac4465f53850987f6062a732dBrian Paul BlocksToErase.push_back(BB); 1603da62bcecfb92978d7243928cfa0fb076b3de762dBrian Paul else 1604738318bb75dea8dac4465f53850987f6062a732dBrian Paul new UnreachableInst(BB); 1605738318bb75dea8dac4465f53850987f6062a732dBrian Paul 1606738318bb75dea8dac4465f53850987f6062a732dBrian Paul } else { 1607738318bb75dea8dac4465f53850987f6062a732dBrian Paul for (BasicBlock::iterator BI = BB->begin(), E = BB->end(); BI != E; ) { 1608fc4b44399a07a7a7559f20ceab8a791209b4d875Brian Paul Instruction *Inst = BI++; 16095ac93f86210eb5c2a8dee74ec19b0ecd54376863Keith Whitwell if (Inst->getType() != Type::VoidTy) { 1610738318bb75dea8dac4465f53850987f6062a732dBrian Paul LatticeVal &IV = Values[Inst]; 1611738318bb75dea8dac4465f53850987f6062a732dBrian Paul if (IV.isConstant() || IV.isUndefined() && 1612738318bb75dea8dac4465f53850987f6062a732dBrian Paul !isa<TerminatorInst>(Inst)) { 1613738318bb75dea8dac4465f53850987f6062a732dBrian Paul Constant *Const = IV.isConstant() 1614d8419c730e73c3be2eadfb0bee176ab06885766aBrian Paul ? IV.getConstant() : UndefValue::get(Inst->getType()); 1615bb372f1c9bc08e8b0dca983cb4ba36b2f2f039fbIan Romanick DOUT << " Constant: " << *Const << " = " << *Inst; 1616bb372f1c9bc08e8b0dca983cb4ba36b2f2f039fbIan Romanick 1617bb372f1c9bc08e8b0dca983cb4ba36b2f2f039fbIan Romanick // Replaces all of the uses of a variable with uses of the 1618bb372f1c9bc08e8b0dca983cb4ba36b2f2f039fbIan Romanick // constant. 1619bb372f1c9bc08e8b0dca983cb4ba36b2f2f039fbIan Romanick Inst->replaceAllUsesWith(Const); 1620bb372f1c9bc08e8b0dca983cb4ba36b2f2f039fbIan Romanick 1621738318bb75dea8dac4465f53850987f6062a732dBrian Paul // Delete the instruction. 162208836341788a9f9d638d9dc8328510ccd18ddeb5Brian Paul if (!isa<TerminatorInst>(Inst) && !isa<CallInst>(Inst)) 1623c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul BB->getInstList().erase(Inst); 1624afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg 1625c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul // Hey, we just changed something! 1626c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul MadeChanges = true; 1627bb372f1c9bc08e8b0dca983cb4ba36b2f2f039fbIan Romanick ++IPNumInstRemoved; 1628bb372f1c9bc08e8b0dca983cb4ba36b2f2f039fbIan Romanick } 1629bb372f1c9bc08e8b0dca983cb4ba36b2f2f039fbIan Romanick } 1630bb372f1c9bc08e8b0dca983cb4ba36b2f2f039fbIan Romanick } 1631bb372f1c9bc08e8b0dca983cb4ba36b2f2f039fbIan Romanick } 1632bb372f1c9bc08e8b0dca983cb4ba36b2f2f039fbIan Romanick 1633bb372f1c9bc08e8b0dca983cb4ba36b2f2f039fbIan Romanick // Now that all instructions in the function are constant folded, erase dead 163408836341788a9f9d638d9dc8328510ccd18ddeb5Brian Paul // blocks, because we can now use ConstantFoldTerminator to get rid of 1635c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul // in-edges. 1636afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg for (unsigned i = 0, e = BlocksToErase.size(); i != e; ++i) { 1637c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul // If there are any PHI nodes in this successor, drop entries for BB now. 1638c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul BasicBlock *DeadBB = BlocksToErase[i]; 1639738318bb75dea8dac4465f53850987f6062a732dBrian Paul while (!DeadBB->use_empty()) { 1640c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul Instruction *I = cast<Instruction>(DeadBB->use_back()); 1641c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul bool Folded = ConstantFoldTerminator(I->getParent()); 1642afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg if (!Folded) { 1643738318bb75dea8dac4465f53850987f6062a732dBrian Paul // The constant folder may not have been able to fold the terminator 1644738318bb75dea8dac4465f53850987f6062a732dBrian Paul // if this is a branch or switch on undef. Fold it manually as a 16454e9676fb13f60ecdbc247b120031f18cd3febcb0Brian Paul // branch to the first successor. 1646c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul if (BranchInst *BI = dyn_cast<BranchInst>(I)) { 1647c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul assert(BI->isConditional() && isa<UndefValue>(BI->getCondition()) && 1648afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg "Branch should be foldable!"); 1649c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul } else if (SwitchInst *SI = dyn_cast<SwitchInst>(I)) { 16504e9676fb13f60ecdbc247b120031f18cd3febcb0Brian Paul assert(isa<UndefValue>(SI->getCondition()) && "Switch should fold"); 16514e9676fb13f60ecdbc247b120031f18cd3febcb0Brian Paul } else { 1652c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul assert(0 && "Didn't fold away reference to block!"); 1653c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul } 1654c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul 16554e9676fb13f60ecdbc247b120031f18cd3febcb0Brian Paul // Make this an uncond branch to the first successor. 16564e9676fb13f60ecdbc247b120031f18cd3febcb0Brian Paul TerminatorInst *TI = I->getParent()->getTerminator(); 1657c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul new BranchInst(TI->getSuccessor(0), TI); 1658c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul 1659c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul // Remove entries in successor phi nodes to remove edges. 1660738318bb75dea8dac4465f53850987f6062a732dBrian Paul for (unsigned i = 1, e = TI->getNumSuccessors(); i != e; ++i) 1661738318bb75dea8dac4465f53850987f6062a732dBrian Paul TI->getSuccessor(i)->removePredecessor(TI->getParent()); 1662c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul 1663c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul // Remove the old terminator. 1664afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg TI->eraseFromParent(); 16655ac93f86210eb5c2a8dee74ec19b0ecd54376863Keith Whitwell } 1666326b981d3fafbf0cc253d2b224f0c9ad307038a3Brian Paul } 1667326b981d3fafbf0cc253d2b224f0c9ad307038a3Brian Paul 1668326b981d3fafbf0cc253d2b224f0c9ad307038a3Brian Paul // Finally, delete the basic block. 1669326b981d3fafbf0cc253d2b224f0c9ad307038a3Brian Paul F->getBasicBlockList().erase(DeadBB); 1670326b981d3fafbf0cc253d2b224f0c9ad307038a3Brian Paul } 1671326b981d3fafbf0cc253d2b224f0c9ad307038a3Brian Paul BlocksToErase.clear(); 16728a8919a7ddd2348c4a4cbcbab2358c49e47e2ea5Brian Paul } 16738a8919a7ddd2348c4a4cbcbab2358c49e47e2ea5Brian Paul 16745ac93f86210eb5c2a8dee74ec19b0ecd54376863Keith Whitwell // If we inferred constant or undef return values for a function, we replaced 16755ac93f86210eb5c2a8dee74ec19b0ecd54376863Keith Whitwell // all call uses with the inferred value. This means we don't need to bother 16765ac93f86210eb5c2a8dee74ec19b0ecd54376863Keith Whitwell // actually returning anything from the function. Replace all return 16775ac93f86210eb5c2a8dee74ec19b0ecd54376863Keith Whitwell // instructions with return undef. 16785ac93f86210eb5c2a8dee74ec19b0ecd54376863Keith Whitwell const DenseMap<Function*, LatticeVal> &RV =Solver.getTrackedFunctionRetVals(); 1679f2718b0966f54049056e16e7cca08718341557b2Brian Paul for (DenseMap<Function*, LatticeVal>::const_iterator I = RV.begin(), 1680d580c0c8f7cad69b808118ef2aa6161f62f160d8Brian Paul E = RV.end(); I != E; ++I) 1681d580c0c8f7cad69b808118ef2aa6161f62f160d8Brian Paul if (!I->second.isOverdefined() && 1682d580c0c8f7cad69b808118ef2aa6161f62f160d8Brian Paul I->first->getReturnType() != Type::VoidTy) { 1683d580c0c8f7cad69b808118ef2aa6161f62f160d8Brian Paul Function *F = I->first; 1684d580c0c8f7cad69b808118ef2aa6161f62f160d8Brian Paul for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) 16855ac93f86210eb5c2a8dee74ec19b0ecd54376863Keith Whitwell if (ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator())) 1686f9995b30756140724f41daf963fa06167912be7fKristian Høgsberg if (!isa<UndefValue>(RI->getOperand(0))) 16875ac93f86210eb5c2a8dee74ec19b0ecd54376863Keith Whitwell RI->setOperand(0, UndefValue::get(F->getReturnType())); 16885ac93f86210eb5c2a8dee74ec19b0ecd54376863Keith Whitwell } 16895ac93f86210eb5c2a8dee74ec19b0ecd54376863Keith Whitwell 16905ac93f86210eb5c2a8dee74ec19b0ecd54376863Keith Whitwell // If we infered constant or undef values for globals variables, we can delete 16915ac93f86210eb5c2a8dee74ec19b0ecd54376863Keith Whitwell // the global and any stores that remain to it. 16925ac93f86210eb5c2a8dee74ec19b0ecd54376863Keith Whitwell const DenseMap<GlobalVariable*, LatticeVal> &TG = Solver.getTrackedGlobals(); 1693c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul for (DenseMap<GlobalVariable*, LatticeVal>::const_iterator I = TG.begin(), 1694738318bb75dea8dac4465f53850987f6062a732dBrian Paul E = TG.end(); I != E; ++I) { 1695738318bb75dea8dac4465f53850987f6062a732dBrian Paul GlobalVariable *GV = I->first; 1696c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul assert(!I->second.isOverdefined() && 1697c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul "Overdefined values should have been taken out of the map!"); 1698afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg DOUT << "Found that GV '" << GV->getName()<< "' is constant!\n"; 1699c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul while (!GV->use_empty()) { 1700738318bb75dea8dac4465f53850987f6062a732dBrian Paul StoreInst *SI = cast<StoreInst>(GV->use_back()); 1701738318bb75dea8dac4465f53850987f6062a732dBrian Paul SI->eraseFromParent(); 1702c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul } 1703afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg M.getGlobalList().erase(GV); 1704c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul ++IPNumGlobalConst; 1705738318bb75dea8dac4465f53850987f6062a732dBrian Paul } 1706738318bb75dea8dac4465f53850987f6062a732dBrian Paul 1707c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul return MadeChanges; 1708c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul} 1709c3f0a511a725c7b3d3d7d93b1955aaaa2bb32f0dBrian Paul