blob: 44634cd7c3b5873b50da1089767ae027798ca03b [file] [log] [blame]
Misha Brukman373086d2003-05-20 21:01:22 +00001//===- SCCP.cpp - Sparse Conditional Constant Propagation -----------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner347389d2001-06-27 23:38:11 +00009//
Misha Brukman373086d2003-05-20 21:01:22 +000010// This file implements sparse conditional constant propagation and merging:
Chris Lattner347389d2001-06-27 23:38:11 +000011//
12// Specifically, this:
13// * Assumes values are constant unless proven otherwise
14// * Assumes BasicBlocks are dead unless proven otherwise
15// * Proves values to be constant, and replaces them with constants
Chris Lattnerdd6522e2002-08-30 23:39:00 +000016// * Proves conditional branches to be unconditional
Chris Lattner347389d2001-06-27 23:38:11 +000017//
18// Notice that:
19// * This pass has a habit of making definitions be dead. It is a good idea
20// to to run a DCE pass sometime after running this pass.
21//
22//===----------------------------------------------------------------------===//
23
Chris Lattner4f031622004-11-15 05:03:30 +000024#define DEBUG_TYPE "sccp"
Chris Lattnerb4cfa7f2002-05-07 20:03:00 +000025#include "llvm/Transforms/Scalar.h"
Chris Lattnerb4394642004-12-10 08:02:06 +000026#include "llvm/Transforms/IPO.h"
Chris Lattner0fe5b322004-01-12 17:43:40 +000027#include "llvm/Constants.h"
Chris Lattner91dbae62004-12-11 05:15:59 +000028#include "llvm/DerivedTypes.h"
Chris Lattnercccc5c72003-04-25 02:50:03 +000029#include "llvm/Instructions.h"
Chris Lattner04805fa2002-02-26 21:46:54 +000030#include "llvm/Pass.h"
Chris Lattner6e560792002-04-18 15:13:15 +000031#include "llvm/Support/InstVisitor.h"
Chris Lattnerff9362a2004-04-13 19:43:54 +000032#include "llvm/Transforms/Utils/Local.h"
Chris Lattnerb4394642004-12-10 08:02:06 +000033#include "llvm/Support/CallSite.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000034#include "llvm/Support/Debug.h"
35#include "llvm/ADT/hash_map"
36#include "llvm/ADT/Statistic.h"
37#include "llvm/ADT/STLExtras.h"
Chris Lattner347389d2001-06-27 23:38:11 +000038#include <algorithm>
Chris Lattnerc597b8a2006-01-22 23:32:06 +000039#include <iostream>
Chris Lattner347389d2001-06-27 23:38:11 +000040#include <set>
Chris Lattner49525f82004-01-09 06:02:20 +000041using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000042
Chris Lattner4f031622004-11-15 05:03:30 +000043// LatticeVal class - This class represents the different lattice values that an
Chris Lattnerc8e66542002-04-27 06:56:12 +000044// instruction may occupy. It is a simple class with value semantics.
Chris Lattner347389d2001-06-27 23:38:11 +000045//
Chris Lattner7d325382002-04-29 21:26:08 +000046namespace {
Chris Lattnerbf3a0992002-10-01 22:38:41 +000047
Chris Lattner4f031622004-11-15 05:03:30 +000048class LatticeVal {
Misha Brukmanb1c93172005-04-21 23:48:37 +000049 enum {
Chris Lattner3462ae32001-12-03 22:26:30 +000050 undefined, // This instruction has no known value
51 constant, // This instruction has a constant value
Chris Lattner3462ae32001-12-03 22:26:30 +000052 overdefined // This instruction has an unknown value
53 } LatticeValue; // The current lattice position
54 Constant *ConstantVal; // If Constant value, the current value
Chris Lattner347389d2001-06-27 23:38:11 +000055public:
Chris Lattner4f031622004-11-15 05:03:30 +000056 inline LatticeVal() : LatticeValue(undefined), ConstantVal(0) {}
Chris Lattner347389d2001-06-27 23:38:11 +000057
58 // markOverdefined - Return true if this is a new status to be in...
59 inline bool markOverdefined() {
Chris Lattner3462ae32001-12-03 22:26:30 +000060 if (LatticeValue != overdefined) {
61 LatticeValue = overdefined;
Chris Lattner347389d2001-06-27 23:38:11 +000062 return true;
63 }
64 return false;
65 }
66
67 // markConstant - Return true if this is a new status for us...
Chris Lattner3462ae32001-12-03 22:26:30 +000068 inline bool markConstant(Constant *V) {
69 if (LatticeValue != constant) {
70 LatticeValue = constant;
Chris Lattner347389d2001-06-27 23:38:11 +000071 ConstantVal = V;
72 return true;
73 } else {
Chris Lattnerdae05dc2001-09-07 16:43:22 +000074 assert(ConstantVal == V && "Marking constant with different value");
Chris Lattner347389d2001-06-27 23:38:11 +000075 }
76 return false;
77 }
78
Chris Lattner3462ae32001-12-03 22:26:30 +000079 inline bool isUndefined() const { return LatticeValue == undefined; }
80 inline bool isConstant() const { return LatticeValue == constant; }
81 inline bool isOverdefined() const { return LatticeValue == overdefined; }
Chris Lattner347389d2001-06-27 23:38:11 +000082
Chris Lattner05fe6842004-01-12 03:57:30 +000083 inline Constant *getConstant() const {
84 assert(isConstant() && "Cannot get the constant of a non-constant!");
85 return ConstantVal;
86 }
Chris Lattner347389d2001-06-27 23:38:11 +000087};
88
Chris Lattner7d325382002-04-29 21:26:08 +000089} // end anonymous namespace
Chris Lattner347389d2001-06-27 23:38:11 +000090
91
92//===----------------------------------------------------------------------===//
Chris Lattner347389d2001-06-27 23:38:11 +000093//
Chris Lattner074be1f2004-11-15 04:44:20 +000094/// SCCPSolver - This class is a general purpose solver for Sparse Conditional
95/// Constant Propagation.
96///
97class SCCPSolver : public InstVisitor<SCCPSolver> {
Chris Lattner7f74a562002-01-20 22:54:45 +000098 std::set<BasicBlock*> BBExecutable;// The basic blocks that are executable
Chris Lattner4f031622004-11-15 05:03:30 +000099 hash_map<Value*, LatticeVal> ValueState; // The state each value is in...
Chris Lattner347389d2001-06-27 23:38:11 +0000100
Chris Lattner91dbae62004-12-11 05:15:59 +0000101 /// GlobalValue - If we are tracking any values for the contents of a global
102 /// variable, we keep a mapping from the constant accessor to the element of
103 /// the global, to the currently known value. If the value becomes
104 /// overdefined, it's entry is simply removed from this map.
105 hash_map<GlobalVariable*, LatticeVal> TrackedGlobals;
106
Chris Lattnerb4394642004-12-10 08:02:06 +0000107 /// TrackedFunctionRetVals - If we are tracking arguments into and the return
108 /// value out of a function, it will have an entry in this map, indicating
109 /// what the known return value for the function is.
110 hash_map<Function*, LatticeVal> TrackedFunctionRetVals;
111
Chris Lattnerd79334d2004-07-15 23:36:43 +0000112 // The reason for two worklists is that overdefined is the lowest state
113 // on the lattice, and moving things to overdefined as fast as possible
114 // makes SCCP converge much faster.
115 // By having a separate worklist, we accomplish this because everything
116 // possibly overdefined will become overdefined at the soonest possible
117 // point.
Chris Lattnerb4394642004-12-10 08:02:06 +0000118 std::vector<Value*> OverdefinedInstWorkList;
119 std::vector<Value*> InstWorkList;
Chris Lattnerd79334d2004-07-15 23:36:43 +0000120
121
Chris Lattner7f74a562002-01-20 22:54:45 +0000122 std::vector<BasicBlock*> BBWorkList; // The BasicBlock work list
Chris Lattner0bbbe5d2003-10-08 16:55:34 +0000123
Chris Lattner05fe6842004-01-12 03:57:30 +0000124 /// UsersOfOverdefinedPHIs - Keep track of any users of PHI nodes that are not
125 /// overdefined, despite the fact that the PHI node is overdefined.
126 std::multimap<PHINode*, Instruction*> UsersOfOverdefinedPHIs;
127
Chris Lattner0bbbe5d2003-10-08 16:55:34 +0000128 /// KnownFeasibleEdges - Entries in this set are edges which have already had
129 /// PHI nodes retriggered.
130 typedef std::pair<BasicBlock*,BasicBlock*> Edge;
131 std::set<Edge> KnownFeasibleEdges;
Chris Lattner347389d2001-06-27 23:38:11 +0000132public:
133
Chris Lattner074be1f2004-11-15 04:44:20 +0000134 /// MarkBlockExecutable - This method can be used by clients to mark all of
135 /// the blocks that are known to be intrinsically live in the processed unit.
136 void MarkBlockExecutable(BasicBlock *BB) {
137 DEBUG(std::cerr << "Marking Block Executable: " << BB->getName() << "\n");
138 BBExecutable.insert(BB); // Basic block is executable!
139 BBWorkList.push_back(BB); // Add the block to the work list!
Chris Lattner7d325382002-04-29 21:26:08 +0000140 }
141
Chris Lattner91dbae62004-12-11 05:15:59 +0000142 /// TrackValueOfGlobalVariable - Clients can use this method to
Chris Lattnerb4394642004-12-10 08:02:06 +0000143 /// inform the SCCPSolver that it should track loads and stores to the
144 /// specified global variable if it can. This is only legal to call if
145 /// performing Interprocedural SCCP.
Chris Lattner91dbae62004-12-11 05:15:59 +0000146 void TrackValueOfGlobalVariable(GlobalVariable *GV) {
147 const Type *ElTy = GV->getType()->getElementType();
148 if (ElTy->isFirstClassType()) {
149 LatticeVal &IV = TrackedGlobals[GV];
150 if (!isa<UndefValue>(GV->getInitializer()))
151 IV.markConstant(GV->getInitializer());
152 }
153 }
Chris Lattnerb4394642004-12-10 08:02:06 +0000154
155 /// AddTrackedFunction - If the SCCP solver is supposed to track calls into
156 /// and out of the specified function (which cannot have its address taken),
157 /// this method must be called.
158 void AddTrackedFunction(Function *F) {
159 assert(F->hasInternalLinkage() && "Can only track internal functions!");
160 // Add an entry, F -> undef.
161 TrackedFunctionRetVals[F];
162 }
163
Chris Lattner074be1f2004-11-15 04:44:20 +0000164 /// Solve - Solve for constants and executable blocks.
165 ///
166 void Solve();
Chris Lattner347389d2001-06-27 23:38:11 +0000167
Chris Lattner7285f432004-12-10 20:41:50 +0000168 /// ResolveBranchesIn - While solving the dataflow for a function, we assume
169 /// that branches on undef values cannot reach any of their successors.
170 /// However, this is not a safe assumption. After we solve dataflow, this
171 /// method should be use to handle this. If this returns true, the solver
172 /// should be rerun.
173 bool ResolveBranchesIn(Function &F);
174
Chris Lattner074be1f2004-11-15 04:44:20 +0000175 /// getExecutableBlocks - Once we have solved for constants, return the set of
176 /// blocks that is known to be executable.
177 std::set<BasicBlock*> &getExecutableBlocks() {
178 return BBExecutable;
179 }
180
181 /// getValueMapping - Once we have solved for constants, return the mapping of
Chris Lattner4f031622004-11-15 05:03:30 +0000182 /// LLVM values to LatticeVals.
183 hash_map<Value*, LatticeVal> &getValueMapping() {
Chris Lattner074be1f2004-11-15 04:44:20 +0000184 return ValueState;
185 }
186
Chris Lattner99e12952004-12-11 02:53:57 +0000187 /// getTrackedFunctionRetVals - Get the inferred return value map.
188 ///
189 const hash_map<Function*, LatticeVal> &getTrackedFunctionRetVals() {
190 return TrackedFunctionRetVals;
191 }
192
Chris Lattner91dbae62004-12-11 05:15:59 +0000193 /// getTrackedGlobals - Get and return the set of inferred initializers for
194 /// global variables.
195 const hash_map<GlobalVariable*, LatticeVal> &getTrackedGlobals() {
196 return TrackedGlobals;
197 }
198
Chris Lattner99e12952004-12-11 02:53:57 +0000199
Chris Lattner347389d2001-06-27 23:38:11 +0000200private:
Chris Lattnerd79334d2004-07-15 23:36:43 +0000201 // markConstant - Make a value be marked as "constant". If the value
Misha Brukmanb1c93172005-04-21 23:48:37 +0000202 // is not already a constant, add it to the instruction work list so that
Chris Lattner347389d2001-06-27 23:38:11 +0000203 // the users of the instruction are updated later.
204 //
Chris Lattnerb4394642004-12-10 08:02:06 +0000205 inline void markConstant(LatticeVal &IV, Value *V, Constant *C) {
Chris Lattner7324f7c2003-10-08 16:21:03 +0000206 if (IV.markConstant(C)) {
Chris Lattnerb4394642004-12-10 08:02:06 +0000207 DEBUG(std::cerr << "markConstant: " << *C << ": " << *V);
208 InstWorkList.push_back(V);
Chris Lattner347389d2001-06-27 23:38:11 +0000209 }
Chris Lattner7324f7c2003-10-08 16:21:03 +0000210 }
Chris Lattnerb4394642004-12-10 08:02:06 +0000211 inline void markConstant(Value *V, Constant *C) {
212 markConstant(ValueState[V], V, C);
Chris Lattner347389d2001-06-27 23:38:11 +0000213 }
214
Chris Lattnerd79334d2004-07-15 23:36:43 +0000215 // markOverdefined - Make a value be marked as "overdefined". If the
Misha Brukmanb1c93172005-04-21 23:48:37 +0000216 // value is not already overdefined, add it to the overdefined instruction
Chris Lattnerd79334d2004-07-15 23:36:43 +0000217 // work list so that the users of the instruction are updated later.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000218
Chris Lattnerb4394642004-12-10 08:02:06 +0000219 inline void markOverdefined(LatticeVal &IV, Value *V) {
Chris Lattner7324f7c2003-10-08 16:21:03 +0000220 if (IV.markOverdefined()) {
Chris Lattner2f687fd2004-12-11 06:05:53 +0000221 DEBUG(std::cerr << "markOverdefined: ";
222 if (Function *F = dyn_cast<Function>(V))
223 std::cerr << "Function '" << F->getName() << "'\n";
224 else
225 std::cerr << *V);
Chris Lattner074be1f2004-11-15 04:44:20 +0000226 // Only instructions go on the work list
Chris Lattnerb4394642004-12-10 08:02:06 +0000227 OverdefinedInstWorkList.push_back(V);
Chris Lattner347389d2001-06-27 23:38:11 +0000228 }
Chris Lattner7324f7c2003-10-08 16:21:03 +0000229 }
Chris Lattnerb4394642004-12-10 08:02:06 +0000230 inline void markOverdefined(Value *V) {
231 markOverdefined(ValueState[V], V);
232 }
233
234 inline void mergeInValue(LatticeVal &IV, Value *V, LatticeVal &MergeWithV) {
235 if (IV.isOverdefined() || MergeWithV.isUndefined())
236 return; // Noop.
237 if (MergeWithV.isOverdefined())
238 markOverdefined(IV, V);
239 else if (IV.isUndefined())
240 markConstant(IV, V, MergeWithV.getConstant());
241 else if (IV.getConstant() != MergeWithV.getConstant())
242 markOverdefined(IV, V);
Chris Lattner347389d2001-06-27 23:38:11 +0000243 }
Chris Lattner06a0ed12006-02-08 02:38:11 +0000244
245 inline void mergeInValue(Value *V, LatticeVal &MergeWithV) {
246 return mergeInValue(ValueState[V], V, MergeWithV);
247 }
248
Chris Lattner347389d2001-06-27 23:38:11 +0000249
Chris Lattner4f031622004-11-15 05:03:30 +0000250 // getValueState - Return the LatticeVal object that corresponds to the value.
Misha Brukman7eb05a12003-08-18 14:43:39 +0000251 // This function is necessary because not all values should start out in the
Chris Lattner2e9fa6d2002-04-09 19:48:49 +0000252 // underdefined state... Argument's should be overdefined, and
Chris Lattner57698e22002-03-26 18:01:55 +0000253 // constants should be marked as constants. If a value is not known to be an
Chris Lattner347389d2001-06-27 23:38:11 +0000254 // Instruction object, then use this accessor to get its value from the map.
255 //
Chris Lattner4f031622004-11-15 05:03:30 +0000256 inline LatticeVal &getValueState(Value *V) {
257 hash_map<Value*, LatticeVal>::iterator I = ValueState.find(V);
Chris Lattner347389d2001-06-27 23:38:11 +0000258 if (I != ValueState.end()) return I->second; // Common case, in the map
Chris Lattner646354b2004-10-16 18:09:41 +0000259
Chris Lattnerd18c16b2004-11-15 05:45:33 +0000260 if (Constant *CPV = dyn_cast<Constant>(V)) {
261 if (isa<UndefValue>(V)) {
262 // Nothing to do, remain undefined.
263 } else {
264 ValueState[CPV].markConstant(CPV); // Constants are constant
265 }
Chris Lattnerdd6522e2002-08-30 23:39:00 +0000266 }
Chris Lattner347389d2001-06-27 23:38:11 +0000267 // All others are underdefined by default...
268 return ValueState[V];
269 }
270
Misha Brukmanb1c93172005-04-21 23:48:37 +0000271 // markEdgeExecutable - Mark a basic block as executable, adding it to the BB
Chris Lattner347389d2001-06-27 23:38:11 +0000272 // work list if it is not already executable...
Misha Brukmanb1c93172005-04-21 23:48:37 +0000273 //
Chris Lattner0bbbe5d2003-10-08 16:55:34 +0000274 void markEdgeExecutable(BasicBlock *Source, BasicBlock *Dest) {
275 if (!KnownFeasibleEdges.insert(Edge(Source, Dest)).second)
276 return; // This edge is already known to be executable!
277
278 if (BBExecutable.count(Dest)) {
279 DEBUG(std::cerr << "Marking Edge Executable: " << Source->getName()
280 << " -> " << Dest->getName() << "\n");
281
282 // The destination is already executable, but we just made an edge
Chris Lattner35e56e72003-10-08 16:56:11 +0000283 // feasible that wasn't before. Revisit the PHI nodes in the block
284 // because they have potentially new operands.
Chris Lattnerb4394642004-12-10 08:02:06 +0000285 for (BasicBlock::iterator I = Dest->begin(); isa<PHINode>(I); ++I)
286 visitPHINode(*cast<PHINode>(I));
Chris Lattnercccc5c72003-04-25 02:50:03 +0000287
288 } else {
Chris Lattner074be1f2004-11-15 04:44:20 +0000289 MarkBlockExecutable(Dest);
Chris Lattnercccc5c72003-04-25 02:50:03 +0000290 }
Chris Lattner347389d2001-06-27 23:38:11 +0000291 }
292
Chris Lattner074be1f2004-11-15 04:44:20 +0000293 // getFeasibleSuccessors - Return a vector of booleans to indicate which
294 // successors are reachable from a given terminator instruction.
295 //
296 void getFeasibleSuccessors(TerminatorInst &TI, std::vector<bool> &Succs);
297
298 // isEdgeFeasible - Return true if the control flow edge from the 'From' basic
299 // block to the 'To' basic block is currently feasible...
300 //
301 bool isEdgeFeasible(BasicBlock *From, BasicBlock *To);
302
303 // OperandChangedState - This method is invoked on all of the users of an
304 // instruction that was just changed state somehow.... Based on this
305 // information, we need to update the specified user of this instruction.
306 //
307 void OperandChangedState(User *U) {
308 // Only instructions use other variable values!
309 Instruction &I = cast<Instruction>(*U);
310 if (BBExecutable.count(I.getParent())) // Inst is executable?
311 visit(I);
312 }
313
314private:
315 friend class InstVisitor<SCCPSolver>;
Chris Lattner347389d2001-06-27 23:38:11 +0000316
Misha Brukmanb1c93172005-04-21 23:48:37 +0000317 // visit implementations - Something changed in this instruction... Either an
Chris Lattner10b250e2001-06-29 23:56:23 +0000318 // operand made a transition, or the instruction is newly executable. Change
319 // the value type of I to reflect these changes if appropriate.
320 //
Chris Lattner113f4f42002-06-25 16:13:24 +0000321 void visitPHINode(PHINode &I);
Chris Lattner6e560792002-04-18 15:13:15 +0000322
323 // Terminators
Chris Lattnerb4394642004-12-10 08:02:06 +0000324 void visitReturnInst(ReturnInst &I);
Chris Lattner113f4f42002-06-25 16:13:24 +0000325 void visitTerminatorInst(TerminatorInst &TI);
Chris Lattner6e560792002-04-18 15:13:15 +0000326
Chris Lattner6e1a1b12002-08-14 17:53:45 +0000327 void visitCastInst(CastInst &I);
Chris Lattner59db22d2004-03-12 05:52:44 +0000328 void visitSelectInst(SelectInst &I);
Chris Lattner113f4f42002-06-25 16:13:24 +0000329 void visitBinaryOperator(Instruction &I);
330 void visitShiftInst(ShiftInst &I) { visitBinaryOperator(I); }
Robert Bocchinobd518d12006-01-10 19:05:05 +0000331 void visitExtractElementInst(ExtractElementInst &I);
Robert Bocchino6dce2502006-01-17 20:06:55 +0000332 void visitInsertElementInst(InsertElementInst &I);
Chris Lattner17bd6052006-04-08 01:19:12 +0000333 void visitShuffleVectorInst(ShuffleVectorInst &I);
Chris Lattner6e560792002-04-18 15:13:15 +0000334
335 // Instructions that cannot be folded away...
Chris Lattner91dbae62004-12-11 05:15:59 +0000336 void visitStoreInst (Instruction &I);
Chris Lattner49f74522004-01-12 04:29:41 +0000337 void visitLoadInst (LoadInst &I);
Chris Lattnerdd6522e2002-08-30 23:39:00 +0000338 void visitGetElementPtrInst(GetElementPtrInst &I);
Chris Lattnerb4394642004-12-10 08:02:06 +0000339 void visitCallInst (CallInst &I) { visitCallSite(CallSite::get(&I)); }
340 void visitInvokeInst (InvokeInst &II) {
341 visitCallSite(CallSite::get(&II));
342 visitTerminatorInst(II);
Chris Lattnerdf741d62003-08-27 01:08:35 +0000343 }
Chris Lattnerb4394642004-12-10 08:02:06 +0000344 void visitCallSite (CallSite CS);
Chris Lattner9c58cf62003-09-08 18:54:55 +0000345 void visitUnwindInst (TerminatorInst &I) { /*returns void*/ }
Chris Lattner646354b2004-10-16 18:09:41 +0000346 void visitUnreachableInst(TerminatorInst &I) { /*returns void*/ }
Chris Lattner113f4f42002-06-25 16:13:24 +0000347 void visitAllocationInst(Instruction &I) { markOverdefined(&I); }
Chris Lattnerf0fc9be2003-10-18 05:56:52 +0000348 void visitVANextInst (Instruction &I) { markOverdefined(&I); }
349 void visitVAArgInst (Instruction &I) { markOverdefined(&I); }
Chris Lattner113f4f42002-06-25 16:13:24 +0000350 void visitFreeInst (Instruction &I) { /*returns void*/ }
Chris Lattner6e560792002-04-18 15:13:15 +0000351
Chris Lattner113f4f42002-06-25 16:13:24 +0000352 void visitInstruction(Instruction &I) {
Chris Lattner6e560792002-04-18 15:13:15 +0000353 // If a new instruction is added to LLVM that we don't handle...
Chris Lattnercccc5c72003-04-25 02:50:03 +0000354 std::cerr << "SCCP: Don't know how to handle: " << I;
Chris Lattner113f4f42002-06-25 16:13:24 +0000355 markOverdefined(&I); // Just in case
Chris Lattner6e560792002-04-18 15:13:15 +0000356 }
Chris Lattner10b250e2001-06-29 23:56:23 +0000357};
Chris Lattnerb28b6802002-07-23 18:06:35 +0000358
Chris Lattnerfe6c9ee2002-05-02 21:44:00 +0000359// getFeasibleSuccessors - Return a vector of booleans to indicate which
360// successors are reachable from a given terminator instruction.
361//
Chris Lattner074be1f2004-11-15 04:44:20 +0000362void SCCPSolver::getFeasibleSuccessors(TerminatorInst &TI,
363 std::vector<bool> &Succs) {
Chris Lattnercccc5c72003-04-25 02:50:03 +0000364 Succs.resize(TI.getNumSuccessors());
Chris Lattner113f4f42002-06-25 16:13:24 +0000365 if (BranchInst *BI = dyn_cast<BranchInst>(&TI)) {
Chris Lattnerfe6c9ee2002-05-02 21:44:00 +0000366 if (BI->isUnconditional()) {
367 Succs[0] = true;
368 } else {
Chris Lattner4f031622004-11-15 05:03:30 +0000369 LatticeVal &BCValue = getValueState(BI->getCondition());
Chris Lattnerfe992d42004-01-12 17:40:36 +0000370 if (BCValue.isOverdefined() ||
371 (BCValue.isConstant() && !isa<ConstantBool>(BCValue.getConstant()))) {
372 // Overdefined condition variables, and branches on unfoldable constant
373 // conditions, mean the branch could go either way.
Chris Lattnerfe6c9ee2002-05-02 21:44:00 +0000374 Succs[0] = Succs[1] = true;
375 } else if (BCValue.isConstant()) {
376 // Constant condition variables mean the branch can only go a single way
Chris Lattner6ab03f62006-09-28 23:35:22 +0000377 Succs[BCValue.getConstant() == ConstantBool::getFalse()] = true;
Chris Lattnerfe6c9ee2002-05-02 21:44:00 +0000378 }
379 }
Chris Lattner113f4f42002-06-25 16:13:24 +0000380 } else if (InvokeInst *II = dyn_cast<InvokeInst>(&TI)) {
Chris Lattnerfe6c9ee2002-05-02 21:44:00 +0000381 // Invoke instructions successors are always executable.
382 Succs[0] = Succs[1] = true;
Chris Lattner113f4f42002-06-25 16:13:24 +0000383 } else if (SwitchInst *SI = dyn_cast<SwitchInst>(&TI)) {
Chris Lattner4f031622004-11-15 05:03:30 +0000384 LatticeVal &SCValue = getValueState(SI->getCondition());
Chris Lattnerfe992d42004-01-12 17:40:36 +0000385 if (SCValue.isOverdefined() || // Overdefined condition?
386 (SCValue.isConstant() && !isa<ConstantInt>(SCValue.getConstant()))) {
Chris Lattnerfe6c9ee2002-05-02 21:44:00 +0000387 // All destinations are executable!
Chris Lattner113f4f42002-06-25 16:13:24 +0000388 Succs.assign(TI.getNumSuccessors(), true);
Chris Lattnerfe6c9ee2002-05-02 21:44:00 +0000389 } else if (SCValue.isConstant()) {
390 Constant *CPV = SCValue.getConstant();
391 // Make sure to skip the "default value" which isn't a value
392 for (unsigned i = 1, E = SI->getNumSuccessors(); i != E; ++i) {
393 if (SI->getSuccessorValue(i) == CPV) {// Found the right branch...
394 Succs[i] = true;
395 return;
396 }
397 }
398
399 // Constant value not equal to any of the branches... must execute
400 // default branch then...
401 Succs[0] = true;
402 }
403 } else {
Chris Lattnercccc5c72003-04-25 02:50:03 +0000404 std::cerr << "SCCP: Don't know how to handle: " << TI;
Chris Lattner113f4f42002-06-25 16:13:24 +0000405 Succs.assign(TI.getNumSuccessors(), true);
Chris Lattnerfe6c9ee2002-05-02 21:44:00 +0000406 }
407}
408
409
Chris Lattner13b52e72002-05-02 21:18:01 +0000410// isEdgeFeasible - Return true if the control flow edge from the 'From' basic
411// block to the 'To' basic block is currently feasible...
412//
Chris Lattner074be1f2004-11-15 04:44:20 +0000413bool SCCPSolver::isEdgeFeasible(BasicBlock *From, BasicBlock *To) {
Chris Lattner13b52e72002-05-02 21:18:01 +0000414 assert(BBExecutable.count(To) && "Dest should always be alive!");
415
416 // Make sure the source basic block is executable!!
417 if (!BBExecutable.count(From)) return false;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000418
Chris Lattnerfe6c9ee2002-05-02 21:44:00 +0000419 // Check to make sure this edge itself is actually feasible now...
Chris Lattner71ac22ff2003-10-08 15:47:41 +0000420 TerminatorInst *TI = From->getTerminator();
421 if (BranchInst *BI = dyn_cast<BranchInst>(TI)) {
422 if (BI->isUnconditional())
Chris Lattnerfe6c9ee2002-05-02 21:44:00 +0000423 return true;
Chris Lattner71ac22ff2003-10-08 15:47:41 +0000424 else {
Chris Lattner4f031622004-11-15 05:03:30 +0000425 LatticeVal &BCValue = getValueState(BI->getCondition());
Chris Lattner71ac22ff2003-10-08 15:47:41 +0000426 if (BCValue.isOverdefined()) {
427 // Overdefined condition variables mean the branch could go either way.
428 return true;
429 } else if (BCValue.isConstant()) {
Chris Lattnerfe992d42004-01-12 17:40:36 +0000430 // Not branching on an evaluatable constant?
431 if (!isa<ConstantBool>(BCValue.getConstant())) return true;
432
Chris Lattner71ac22ff2003-10-08 15:47:41 +0000433 // Constant condition variables mean the branch can only go a single way
Misha Brukmanb1c93172005-04-21 23:48:37 +0000434 return BI->getSuccessor(BCValue.getConstant() ==
Chris Lattner6ab03f62006-09-28 23:35:22 +0000435 ConstantBool::getFalse()) == To;
Chris Lattner71ac22ff2003-10-08 15:47:41 +0000436 }
437 return false;
438 }
439 } else if (InvokeInst *II = dyn_cast<InvokeInst>(TI)) {
440 // Invoke instructions successors are always executable.
441 return true;
442 } else if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
Chris Lattner4f031622004-11-15 05:03:30 +0000443 LatticeVal &SCValue = getValueState(SI->getCondition());
Chris Lattner71ac22ff2003-10-08 15:47:41 +0000444 if (SCValue.isOverdefined()) { // Overdefined condition?
445 // All destinations are executable!
446 return true;
447 } else if (SCValue.isConstant()) {
448 Constant *CPV = SCValue.getConstant();
Chris Lattnerfe992d42004-01-12 17:40:36 +0000449 if (!isa<ConstantInt>(CPV))
450 return true; // not a foldable constant?
451
Chris Lattner71ac22ff2003-10-08 15:47:41 +0000452 // Make sure to skip the "default value" which isn't a value
453 for (unsigned i = 1, E = SI->getNumSuccessors(); i != E; ++i)
454 if (SI->getSuccessorValue(i) == CPV) // Found the taken branch...
455 return SI->getSuccessor(i) == To;
456
457 // Constant value not equal to any of the branches... must execute
458 // default branch then...
459 return SI->getDefaultDest() == To;
460 }
461 return false;
462 } else {
463 std::cerr << "Unknown terminator instruction: " << *TI;
464 abort();
465 }
Chris Lattner13b52e72002-05-02 21:18:01 +0000466}
Chris Lattner347389d2001-06-27 23:38:11 +0000467
Chris Lattner6e560792002-04-18 15:13:15 +0000468// visit Implementations - Something changed in this instruction... Either an
Chris Lattner347389d2001-06-27 23:38:11 +0000469// operand made a transition, or the instruction is newly executable. Change
470// the value type of I to reflect these changes if appropriate. This method
471// makes sure to do the following actions:
472//
473// 1. If a phi node merges two constants in, and has conflicting value coming
474// from different branches, or if the PHI node merges in an overdefined
475// value, then the PHI node becomes overdefined.
476// 2. If a phi node merges only constants in, and they all agree on value, the
477// PHI node becomes a constant value equal to that.
478// 3. If V <- x (op) y && isConstant(x) && isConstant(y) V = Constant
479// 4. If V <- x (op) y && (isOverdefined(x) || isOverdefined(y)) V = Overdefined
480// 5. If V <- MEM or V <- CALL or V <- (unknown) then V = Overdefined
481// 6. If a conditional branch has a value that is constant, make the selected
482// destination executable
483// 7. If a conditional branch has a value that is overdefined, make all
484// successors executable.
485//
Chris Lattner074be1f2004-11-15 04:44:20 +0000486void SCCPSolver::visitPHINode(PHINode &PN) {
Chris Lattner4f031622004-11-15 05:03:30 +0000487 LatticeVal &PNIV = getValueState(&PN);
Chris Lattner05fe6842004-01-12 03:57:30 +0000488 if (PNIV.isOverdefined()) {
489 // There may be instructions using this PHI node that are not overdefined
490 // themselves. If so, make sure that they know that the PHI node operand
491 // changed.
492 std::multimap<PHINode*, Instruction*>::iterator I, E;
493 tie(I, E) = UsersOfOverdefinedPHIs.equal_range(&PN);
494 if (I != E) {
495 std::vector<Instruction*> Users;
496 Users.reserve(std::distance(I, E));
497 for (; I != E; ++I) Users.push_back(I->second);
498 while (!Users.empty()) {
499 visit(Users.back());
500 Users.pop_back();
501 }
502 }
503 return; // Quick exit
504 }
Chris Lattner347389d2001-06-27 23:38:11 +0000505
Chris Lattner7a7b1142004-03-16 19:49:59 +0000506 // Super-extra-high-degree PHI nodes are unlikely to ever be marked constant,
507 // and slow us down a lot. Just mark them overdefined.
508 if (PN.getNumIncomingValues() > 64) {
509 markOverdefined(PNIV, &PN);
510 return;
511 }
512
Chris Lattner6e560792002-04-18 15:13:15 +0000513 // Look at all of the executable operands of the PHI node. If any of them
514 // are overdefined, the PHI becomes overdefined as well. If they are all
515 // constant, and they agree with each other, the PHI becomes the identical
516 // constant. If they are constant and don't agree, the PHI is overdefined.
517 // If there are no executable operands, the PHI remains undefined.
518 //
Chris Lattnercccc5c72003-04-25 02:50:03 +0000519 Constant *OperandVal = 0;
520 for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) {
Chris Lattner4f031622004-11-15 05:03:30 +0000521 LatticeVal &IV = getValueState(PN.getIncomingValue(i));
Chris Lattnercccc5c72003-04-25 02:50:03 +0000522 if (IV.isUndefined()) continue; // Doesn't influence PHI node.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000523
Chris Lattner113f4f42002-06-25 16:13:24 +0000524 if (isEdgeFeasible(PN.getIncomingBlock(i), PN.getParent())) {
Chris Lattner7e270582003-06-24 20:29:52 +0000525 if (IV.isOverdefined()) { // PHI node becomes overdefined!
Chris Lattner7324f7c2003-10-08 16:21:03 +0000526 markOverdefined(PNIV, &PN);
Chris Lattner7e270582003-06-24 20:29:52 +0000527 return;
528 }
529
Chris Lattnercccc5c72003-04-25 02:50:03 +0000530 if (OperandVal == 0) { // Grab the first value...
531 OperandVal = IV.getConstant();
Chris Lattner6e560792002-04-18 15:13:15 +0000532 } else { // Another value is being merged in!
533 // There is already a reachable operand. If we conflict with it,
534 // then the PHI node becomes overdefined. If we agree with it, we
535 // can continue on.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000536
Chris Lattner6e560792002-04-18 15:13:15 +0000537 // Check to see if there are two different constants merging...
Chris Lattnercccc5c72003-04-25 02:50:03 +0000538 if (IV.getConstant() != OperandVal) {
Chris Lattner6e560792002-04-18 15:13:15 +0000539 // Yes there is. This means the PHI node is not constant.
540 // You must be overdefined poor PHI.
541 //
Chris Lattner7324f7c2003-10-08 16:21:03 +0000542 markOverdefined(PNIV, &PN); // The PHI node now becomes overdefined
Chris Lattner6e560792002-04-18 15:13:15 +0000543 return; // I'm done analyzing you
Chris Lattnerc4ad64c2001-11-26 18:57:38 +0000544 }
Chris Lattner347389d2001-06-27 23:38:11 +0000545 }
546 }
Chris Lattner347389d2001-06-27 23:38:11 +0000547 }
548
Chris Lattner6e560792002-04-18 15:13:15 +0000549 // If we exited the loop, this means that the PHI node only has constant
Chris Lattnercccc5c72003-04-25 02:50:03 +0000550 // arguments that agree with each other(and OperandVal is the constant) or
551 // OperandVal is null because there are no defined incoming arguments. If
552 // this is the case, the PHI remains undefined.
Chris Lattner347389d2001-06-27 23:38:11 +0000553 //
Chris Lattnercccc5c72003-04-25 02:50:03 +0000554 if (OperandVal)
Misha Brukman8b2bd4e2003-10-10 17:57:28 +0000555 markConstant(PNIV, &PN, OperandVal); // Acquire operand value
Chris Lattner347389d2001-06-27 23:38:11 +0000556}
557
Chris Lattnerb4394642004-12-10 08:02:06 +0000558void SCCPSolver::visitReturnInst(ReturnInst &I) {
559 if (I.getNumOperands() == 0) return; // Ret void
560
561 // If we are tracking the return value of this function, merge it in.
562 Function *F = I.getParent()->getParent();
563 if (F->hasInternalLinkage() && !TrackedFunctionRetVals.empty()) {
564 hash_map<Function*, LatticeVal>::iterator TFRVI =
565 TrackedFunctionRetVals.find(F);
566 if (TFRVI != TrackedFunctionRetVals.end() &&
567 !TFRVI->second.isOverdefined()) {
568 LatticeVal &IV = getValueState(I.getOperand(0));
569 mergeInValue(TFRVI->second, F, IV);
570 }
571 }
572}
573
574
Chris Lattner074be1f2004-11-15 04:44:20 +0000575void SCCPSolver::visitTerminatorInst(TerminatorInst &TI) {
Chris Lattnercccc5c72003-04-25 02:50:03 +0000576 std::vector<bool> SuccFeasible;
Chris Lattnerfe6c9ee2002-05-02 21:44:00 +0000577 getFeasibleSuccessors(TI, SuccFeasible);
Chris Lattner347389d2001-06-27 23:38:11 +0000578
Chris Lattner0bbbe5d2003-10-08 16:55:34 +0000579 BasicBlock *BB = TI.getParent();
580
Chris Lattnerfe6c9ee2002-05-02 21:44:00 +0000581 // Mark all feasible successors executable...
582 for (unsigned i = 0, e = SuccFeasible.size(); i != e; ++i)
Chris Lattner0bbbe5d2003-10-08 16:55:34 +0000583 if (SuccFeasible[i])
584 markEdgeExecutable(BB, TI.getSuccessor(i));
Chris Lattner6e560792002-04-18 15:13:15 +0000585}
586
Chris Lattner074be1f2004-11-15 04:44:20 +0000587void SCCPSolver::visitCastInst(CastInst &I) {
Chris Lattner113f4f42002-06-25 16:13:24 +0000588 Value *V = I.getOperand(0);
Chris Lattner4f031622004-11-15 05:03:30 +0000589 LatticeVal &VState = getValueState(V);
Chris Lattner0fe5b322004-01-12 17:43:40 +0000590 if (VState.isOverdefined()) // Inherit overdefinedness of operand
Chris Lattner113f4f42002-06-25 16:13:24 +0000591 markOverdefined(&I);
Chris Lattner0fe5b322004-01-12 17:43:40 +0000592 else if (VState.isConstant()) // Propagate constant value
593 markConstant(&I, ConstantExpr::getCast(VState.getConstant(), I.getType()));
Chris Lattner6e560792002-04-18 15:13:15 +0000594}
595
Chris Lattner074be1f2004-11-15 04:44:20 +0000596void SCCPSolver::visitSelectInst(SelectInst &I) {
Chris Lattner4f031622004-11-15 05:03:30 +0000597 LatticeVal &CondValue = getValueState(I.getCondition());
Chris Lattner06a0ed12006-02-08 02:38:11 +0000598 if (CondValue.isUndefined())
599 return;
600 if (CondValue.isConstant()) {
Chris Lattner6ab03f62006-09-28 23:35:22 +0000601 if (ConstantBool *CondCB = dyn_cast<ConstantBool>(CondValue.getConstant())){
602 mergeInValue(&I, getValueState(CondCB->getValue() ? I.getTrueValue()
603 : I.getFalseValue()));
Chris Lattner06a0ed12006-02-08 02:38:11 +0000604 return;
605 }
606 }
607
608 // Otherwise, the condition is overdefined or a constant we can't evaluate.
609 // See if we can produce something better than overdefined based on the T/F
610 // value.
611 LatticeVal &TVal = getValueState(I.getTrueValue());
612 LatticeVal &FVal = getValueState(I.getFalseValue());
613
614 // select ?, C, C -> C.
615 if (TVal.isConstant() && FVal.isConstant() &&
616 TVal.getConstant() == FVal.getConstant()) {
617 markConstant(&I, FVal.getConstant());
618 return;
619 }
620
621 if (TVal.isUndefined()) { // select ?, undef, X -> X.
622 mergeInValue(&I, FVal);
623 } else if (FVal.isUndefined()) { // select ?, X, undef -> X.
624 mergeInValue(&I, TVal);
625 } else {
626 markOverdefined(&I);
Chris Lattner59db22d2004-03-12 05:52:44 +0000627 }
628}
629
Chris Lattner6e560792002-04-18 15:13:15 +0000630// Handle BinaryOperators and Shift Instructions...
Chris Lattner074be1f2004-11-15 04:44:20 +0000631void SCCPSolver::visitBinaryOperator(Instruction &I) {
Chris Lattner4f031622004-11-15 05:03:30 +0000632 LatticeVal &IV = ValueState[&I];
Chris Lattner05fe6842004-01-12 03:57:30 +0000633 if (IV.isOverdefined()) return;
634
Chris Lattner4f031622004-11-15 05:03:30 +0000635 LatticeVal &V1State = getValueState(I.getOperand(0));
636 LatticeVal &V2State = getValueState(I.getOperand(1));
Chris Lattner05fe6842004-01-12 03:57:30 +0000637
Chris Lattner6e560792002-04-18 15:13:15 +0000638 if (V1State.isOverdefined() || V2State.isOverdefined()) {
Chris Lattnercbc01612004-12-11 23:15:19 +0000639 // If this is an AND or OR with 0 or -1, it doesn't matter that the other
640 // operand is overdefined.
641 if (I.getOpcode() == Instruction::And || I.getOpcode() == Instruction::Or) {
642 LatticeVal *NonOverdefVal = 0;
643 if (!V1State.isOverdefined()) {
644 NonOverdefVal = &V1State;
645 } else if (!V2State.isOverdefined()) {
646 NonOverdefVal = &V2State;
647 }
648
649 if (NonOverdefVal) {
650 if (NonOverdefVal->isUndefined()) {
651 // Could annihilate value.
652 if (I.getOpcode() == Instruction::And)
653 markConstant(IV, &I, Constant::getNullValue(I.getType()));
654 else
655 markConstant(IV, &I, ConstantInt::getAllOnesValue(I.getType()));
656 return;
657 } else {
658 if (I.getOpcode() == Instruction::And) {
659 if (NonOverdefVal->getConstant()->isNullValue()) {
660 markConstant(IV, &I, NonOverdefVal->getConstant());
661 return; // X or 0 = -1
662 }
663 } else {
664 if (ConstantIntegral *CI =
665 dyn_cast<ConstantIntegral>(NonOverdefVal->getConstant()))
666 if (CI->isAllOnesValue()) {
667 markConstant(IV, &I, NonOverdefVal->getConstant());
668 return; // X or -1 = -1
669 }
670 }
671 }
672 }
673 }
674
675
Chris Lattner05fe6842004-01-12 03:57:30 +0000676 // If both operands are PHI nodes, it is possible that this instruction has
677 // a constant value, despite the fact that the PHI node doesn't. Check for
678 // this condition now.
679 if (PHINode *PN1 = dyn_cast<PHINode>(I.getOperand(0)))
680 if (PHINode *PN2 = dyn_cast<PHINode>(I.getOperand(1)))
681 if (PN1->getParent() == PN2->getParent()) {
682 // Since the two PHI nodes are in the same basic block, they must have
683 // entries for the same predecessors. Walk the predecessor list, and
684 // if all of the incoming values are constants, and the result of
685 // evaluating this expression with all incoming value pairs is the
686 // same, then this expression is a constant even though the PHI node
687 // is not a constant!
Chris Lattner4f031622004-11-15 05:03:30 +0000688 LatticeVal Result;
Chris Lattner05fe6842004-01-12 03:57:30 +0000689 for (unsigned i = 0, e = PN1->getNumIncomingValues(); i != e; ++i) {
Chris Lattner4f031622004-11-15 05:03:30 +0000690 LatticeVal &In1 = getValueState(PN1->getIncomingValue(i));
Chris Lattner05fe6842004-01-12 03:57:30 +0000691 BasicBlock *InBlock = PN1->getIncomingBlock(i);
Chris Lattner4f031622004-11-15 05:03:30 +0000692 LatticeVal &In2 =
693 getValueState(PN2->getIncomingValueForBlock(InBlock));
Chris Lattner05fe6842004-01-12 03:57:30 +0000694
695 if (In1.isOverdefined() || In2.isOverdefined()) {
696 Result.markOverdefined();
697 break; // Cannot fold this operation over the PHI nodes!
698 } else if (In1.isConstant() && In2.isConstant()) {
Chris Lattner1b7d4d72004-01-12 19:08:43 +0000699 Constant *V = ConstantExpr::get(I.getOpcode(), In1.getConstant(),
700 In2.getConstant());
Chris Lattner05fe6842004-01-12 03:57:30 +0000701 if (Result.isUndefined())
Chris Lattner1b7d4d72004-01-12 19:08:43 +0000702 Result.markConstant(V);
703 else if (Result.isConstant() && Result.getConstant() != V) {
Chris Lattner05fe6842004-01-12 03:57:30 +0000704 Result.markOverdefined();
705 break;
706 }
707 }
708 }
709
710 // If we found a constant value here, then we know the instruction is
711 // constant despite the fact that the PHI nodes are overdefined.
712 if (Result.isConstant()) {
713 markConstant(IV, &I, Result.getConstant());
714 // Remember that this instruction is virtually using the PHI node
715 // operands.
716 UsersOfOverdefinedPHIs.insert(std::make_pair(PN1, &I));
717 UsersOfOverdefinedPHIs.insert(std::make_pair(PN2, &I));
718 return;
719 } else if (Result.isUndefined()) {
720 return;
721 }
722
723 // Okay, this really is overdefined now. Since we might have
724 // speculatively thought that this was not overdefined before, and
725 // added ourselves to the UsersOfOverdefinedPHIs list for the PHIs,
726 // make sure to clean out any entries that we put there, for
727 // efficiency.
728 std::multimap<PHINode*, Instruction*>::iterator It, E;
729 tie(It, E) = UsersOfOverdefinedPHIs.equal_range(PN1);
730 while (It != E) {
731 if (It->second == &I) {
732 UsersOfOverdefinedPHIs.erase(It++);
733 } else
734 ++It;
735 }
736 tie(It, E) = UsersOfOverdefinedPHIs.equal_range(PN2);
737 while (It != E) {
738 if (It->second == &I) {
739 UsersOfOverdefinedPHIs.erase(It++);
740 } else
741 ++It;
742 }
743 }
744
745 markOverdefined(IV, &I);
Chris Lattner6e560792002-04-18 15:13:15 +0000746 } else if (V1State.isConstant() && V2State.isConstant()) {
Chris Lattner1b7d4d72004-01-12 19:08:43 +0000747 markConstant(IV, &I, ConstantExpr::get(I.getOpcode(), V1State.getConstant(),
748 V2State.getConstant()));
Chris Lattner6e560792002-04-18 15:13:15 +0000749 }
750}
Chris Lattnerdd6522e2002-08-30 23:39:00 +0000751
Robert Bocchinobd518d12006-01-10 19:05:05 +0000752void SCCPSolver::visitExtractElementInst(ExtractElementInst &I) {
753 LatticeVal &ValState = getValueState(I.getOperand(0));
754 LatticeVal &IdxState = getValueState(I.getOperand(1));
755
756 if (ValState.isOverdefined() || IdxState.isOverdefined())
757 markOverdefined(&I);
758 else if(ValState.isConstant() && IdxState.isConstant())
759 markConstant(&I, ConstantExpr::getExtractElement(ValState.getConstant(),
760 IdxState.getConstant()));
761}
762
Robert Bocchino6dce2502006-01-17 20:06:55 +0000763void SCCPSolver::visitInsertElementInst(InsertElementInst &I) {
764 LatticeVal &ValState = getValueState(I.getOperand(0));
765 LatticeVal &EltState = getValueState(I.getOperand(1));
766 LatticeVal &IdxState = getValueState(I.getOperand(2));
767
768 if (ValState.isOverdefined() || EltState.isOverdefined() ||
769 IdxState.isOverdefined())
770 markOverdefined(&I);
771 else if(ValState.isConstant() && EltState.isConstant() &&
772 IdxState.isConstant())
773 markConstant(&I, ConstantExpr::getInsertElement(ValState.getConstant(),
774 EltState.getConstant(),
775 IdxState.getConstant()));
776 else if (ValState.isUndefined() && EltState.isConstant() &&
777 IdxState.isConstant())
778 markConstant(&I, ConstantExpr::getInsertElement(UndefValue::get(I.getType()),
779 EltState.getConstant(),
780 IdxState.getConstant()));
781}
782
Chris Lattner17bd6052006-04-08 01:19:12 +0000783void SCCPSolver::visitShuffleVectorInst(ShuffleVectorInst &I) {
784 LatticeVal &V1State = getValueState(I.getOperand(0));
785 LatticeVal &V2State = getValueState(I.getOperand(1));
786 LatticeVal &MaskState = getValueState(I.getOperand(2));
787
788 if (MaskState.isUndefined() ||
789 (V1State.isUndefined() && V2State.isUndefined()))
790 return; // Undefined output if mask or both inputs undefined.
791
792 if (V1State.isOverdefined() || V2State.isOverdefined() ||
793 MaskState.isOverdefined()) {
794 markOverdefined(&I);
795 } else {
796 // A mix of constant/undef inputs.
797 Constant *V1 = V1State.isConstant() ?
798 V1State.getConstant() : UndefValue::get(I.getType());
799 Constant *V2 = V2State.isConstant() ?
800 V2State.getConstant() : UndefValue::get(I.getType());
801 Constant *Mask = MaskState.isConstant() ?
802 MaskState.getConstant() : UndefValue::get(I.getOperand(2)->getType());
803 markConstant(&I, ConstantExpr::getShuffleVector(V1, V2, Mask));
804 }
805}
806
Chris Lattnerdd6522e2002-08-30 23:39:00 +0000807// Handle getelementptr instructions... if all operands are constants then we
808// can turn this into a getelementptr ConstantExpr.
809//
Chris Lattner074be1f2004-11-15 04:44:20 +0000810void SCCPSolver::visitGetElementPtrInst(GetElementPtrInst &I) {
Chris Lattner4f031622004-11-15 05:03:30 +0000811 LatticeVal &IV = ValueState[&I];
Chris Lattner49f74522004-01-12 04:29:41 +0000812 if (IV.isOverdefined()) return;
813
Chris Lattnerdd6522e2002-08-30 23:39:00 +0000814 std::vector<Constant*> Operands;
815 Operands.reserve(I.getNumOperands());
816
817 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) {
Chris Lattner4f031622004-11-15 05:03:30 +0000818 LatticeVal &State = getValueState(I.getOperand(i));
Chris Lattnerdd6522e2002-08-30 23:39:00 +0000819 if (State.isUndefined())
820 return; // Operands are not resolved yet...
821 else if (State.isOverdefined()) {
Chris Lattner49f74522004-01-12 04:29:41 +0000822 markOverdefined(IV, &I);
Chris Lattnerdd6522e2002-08-30 23:39:00 +0000823 return;
824 }
825 assert(State.isConstant() && "Unknown state!");
826 Operands.push_back(State.getConstant());
827 }
828
829 Constant *Ptr = Operands[0];
830 Operands.erase(Operands.begin()); // Erase the pointer from idx list...
831
Misha Brukmanb1c93172005-04-21 23:48:37 +0000832 markConstant(IV, &I, ConstantExpr::getGetElementPtr(Ptr, Operands));
Chris Lattnerdd6522e2002-08-30 23:39:00 +0000833}
Brian Gaeke960707c2003-11-11 22:41:34 +0000834
Chris Lattner91dbae62004-12-11 05:15:59 +0000835void SCCPSolver::visitStoreInst(Instruction &SI) {
836 if (TrackedGlobals.empty() || !isa<GlobalVariable>(SI.getOperand(1)))
837 return;
838 GlobalVariable *GV = cast<GlobalVariable>(SI.getOperand(1));
839 hash_map<GlobalVariable*, LatticeVal>::iterator I = TrackedGlobals.find(GV);
840 if (I == TrackedGlobals.end() || I->second.isOverdefined()) return;
841
842 // Get the value we are storing into the global.
843 LatticeVal &PtrVal = getValueState(SI.getOperand(0));
844
845 mergeInValue(I->second, GV, PtrVal);
846 if (I->second.isOverdefined())
847 TrackedGlobals.erase(I); // No need to keep tracking this!
848}
849
850
Chris Lattner49f74522004-01-12 04:29:41 +0000851// Handle load instructions. If the operand is a constant pointer to a constant
852// global, we can replace the load with the loaded constant value!
Chris Lattner074be1f2004-11-15 04:44:20 +0000853void SCCPSolver::visitLoadInst(LoadInst &I) {
Chris Lattner4f031622004-11-15 05:03:30 +0000854 LatticeVal &IV = ValueState[&I];
Chris Lattner49f74522004-01-12 04:29:41 +0000855 if (IV.isOverdefined()) return;
856
Chris Lattner4f031622004-11-15 05:03:30 +0000857 LatticeVal &PtrVal = getValueState(I.getOperand(0));
Chris Lattner49f74522004-01-12 04:29:41 +0000858 if (PtrVal.isUndefined()) return; // The pointer is not resolved yet!
859 if (PtrVal.isConstant() && !I.isVolatile()) {
860 Value *Ptr = PtrVal.getConstant();
Chris Lattner538fee72004-03-07 22:16:24 +0000861 if (isa<ConstantPointerNull>(Ptr)) {
862 // load null -> null
863 markConstant(IV, &I, Constant::getNullValue(I.getType()));
864 return;
865 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000866
Chris Lattner49f74522004-01-12 04:29:41 +0000867 // Transform load (constant global) into the value loaded.
Chris Lattner91dbae62004-12-11 05:15:59 +0000868 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Ptr)) {
869 if (GV->isConstant()) {
870 if (!GV->isExternal()) {
871 markConstant(IV, &I, GV->getInitializer());
872 return;
873 }
874 } else if (!TrackedGlobals.empty()) {
875 // If we are tracking this global, merge in the known value for it.
876 hash_map<GlobalVariable*, LatticeVal>::iterator It =
877 TrackedGlobals.find(GV);
878 if (It != TrackedGlobals.end()) {
879 mergeInValue(IV, &I, It->second);
880 return;
881 }
Chris Lattner49f74522004-01-12 04:29:41 +0000882 }
Chris Lattner91dbae62004-12-11 05:15:59 +0000883 }
Chris Lattner49f74522004-01-12 04:29:41 +0000884
885 // Transform load (constantexpr_GEP global, 0, ...) into the value loaded.
886 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr))
887 if (CE->getOpcode() == Instruction::GetElementPtr)
Jeff Cohen82639852005-04-23 21:38:35 +0000888 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(CE->getOperand(0)))
889 if (GV->isConstant() && !GV->isExternal())
890 if (Constant *V =
Chris Lattner02ae21e2005-09-26 05:28:52 +0000891 ConstantFoldLoadThroughGEPConstantExpr(GV->getInitializer(), CE)) {
Jeff Cohen82639852005-04-23 21:38:35 +0000892 markConstant(IV, &I, V);
893 return;
894 }
Chris Lattner49f74522004-01-12 04:29:41 +0000895 }
896
897 // Otherwise we cannot say for certain what value this load will produce.
898 // Bail out.
899 markOverdefined(IV, &I);
900}
Chris Lattnerff9362a2004-04-13 19:43:54 +0000901
Chris Lattnerb4394642004-12-10 08:02:06 +0000902void SCCPSolver::visitCallSite(CallSite CS) {
903 Function *F = CS.getCalledFunction();
904
905 // If we are tracking this function, we must make sure to bind arguments as
906 // appropriate.
907 hash_map<Function*, LatticeVal>::iterator TFRVI =TrackedFunctionRetVals.end();
908 if (F && F->hasInternalLinkage())
909 TFRVI = TrackedFunctionRetVals.find(F);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000910
Chris Lattnerb4394642004-12-10 08:02:06 +0000911 if (TFRVI != TrackedFunctionRetVals.end()) {
912 // If this is the first call to the function hit, mark its entry block
913 // executable.
914 if (!BBExecutable.count(F->begin()))
915 MarkBlockExecutable(F->begin());
916
917 CallSite::arg_iterator CAI = CS.arg_begin();
Chris Lattner531f9e92005-03-15 04:54:21 +0000918 for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end();
Chris Lattnerb4394642004-12-10 08:02:06 +0000919 AI != E; ++AI, ++CAI) {
920 LatticeVal &IV = ValueState[AI];
921 if (!IV.isOverdefined())
922 mergeInValue(IV, AI, getValueState(*CAI));
923 }
924 }
925 Instruction *I = CS.getInstruction();
926 if (I->getType() == Type::VoidTy) return;
927
928 LatticeVal &IV = ValueState[I];
Chris Lattnerff9362a2004-04-13 19:43:54 +0000929 if (IV.isOverdefined()) return;
930
Chris Lattnerb4394642004-12-10 08:02:06 +0000931 // Propagate the return value of the function to the value of the instruction.
932 if (TFRVI != TrackedFunctionRetVals.end()) {
933 mergeInValue(IV, I, TFRVI->second);
934 return;
935 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000936
Chris Lattnerb4394642004-12-10 08:02:06 +0000937 if (F == 0 || !F->isExternal() || !canConstantFoldCallTo(F)) {
938 markOverdefined(IV, I);
Chris Lattnerff9362a2004-04-13 19:43:54 +0000939 return;
940 }
941
942 std::vector<Constant*> Operands;
Chris Lattnerb4394642004-12-10 08:02:06 +0000943 Operands.reserve(I->getNumOperands()-1);
Chris Lattnerff9362a2004-04-13 19:43:54 +0000944
Chris Lattnerb4394642004-12-10 08:02:06 +0000945 for (CallSite::arg_iterator AI = CS.arg_begin(), E = CS.arg_end();
946 AI != E; ++AI) {
947 LatticeVal &State = getValueState(*AI);
Chris Lattnerff9362a2004-04-13 19:43:54 +0000948 if (State.isUndefined())
949 return; // Operands are not resolved yet...
950 else if (State.isOverdefined()) {
Chris Lattnerb4394642004-12-10 08:02:06 +0000951 markOverdefined(IV, I);
Chris Lattnerff9362a2004-04-13 19:43:54 +0000952 return;
953 }
954 assert(State.isConstant() && "Unknown state!");
955 Operands.push_back(State.getConstant());
956 }
957
958 if (Constant *C = ConstantFoldCall(F, Operands))
Chris Lattnerb4394642004-12-10 08:02:06 +0000959 markConstant(IV, I, C);
Chris Lattnerff9362a2004-04-13 19:43:54 +0000960 else
Chris Lattnerb4394642004-12-10 08:02:06 +0000961 markOverdefined(IV, I);
Chris Lattnerff9362a2004-04-13 19:43:54 +0000962}
Chris Lattner074be1f2004-11-15 04:44:20 +0000963
964
965void SCCPSolver::Solve() {
966 // Process the work lists until they are empty!
Misha Brukmanb1c93172005-04-21 23:48:37 +0000967 while (!BBWorkList.empty() || !InstWorkList.empty() ||
Jeff Cohen82639852005-04-23 21:38:35 +0000968 !OverdefinedInstWorkList.empty()) {
Chris Lattner074be1f2004-11-15 04:44:20 +0000969 // Process the instruction work list...
970 while (!OverdefinedInstWorkList.empty()) {
Chris Lattnerb4394642004-12-10 08:02:06 +0000971 Value *I = OverdefinedInstWorkList.back();
Chris Lattner074be1f2004-11-15 04:44:20 +0000972 OverdefinedInstWorkList.pop_back();
973
Chris Lattnerb4394642004-12-10 08:02:06 +0000974 DEBUG(std::cerr << "\nPopped off OI-WL: " << *I);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000975
Chris Lattner074be1f2004-11-15 04:44:20 +0000976 // "I" got into the work list because it either made the transition from
977 // bottom to constant
978 //
979 // Anything on this worklist that is overdefined need not be visited
980 // since all of its users will have already been marked as overdefined
981 // Update all of the users of this instruction's value...
982 //
983 for (Value::use_iterator UI = I->use_begin(), E = I->use_end();
984 UI != E; ++UI)
985 OperandChangedState(*UI);
986 }
987 // Process the instruction work list...
988 while (!InstWorkList.empty()) {
Chris Lattnerb4394642004-12-10 08:02:06 +0000989 Value *I = InstWorkList.back();
Chris Lattner074be1f2004-11-15 04:44:20 +0000990 InstWorkList.pop_back();
991
992 DEBUG(std::cerr << "\nPopped off I-WL: " << *I);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000993
Chris Lattner074be1f2004-11-15 04:44:20 +0000994 // "I" got into the work list because it either made the transition from
995 // bottom to constant
996 //
997 // Anything on this worklist that is overdefined need not be visited
998 // since all of its users will have already been marked as overdefined.
999 // Update all of the users of this instruction's value...
1000 //
1001 if (!getValueState(I).isOverdefined())
1002 for (Value::use_iterator UI = I->use_begin(), E = I->use_end();
1003 UI != E; ++UI)
1004 OperandChangedState(*UI);
1005 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001006
Chris Lattner074be1f2004-11-15 04:44:20 +00001007 // Process the basic block work list...
1008 while (!BBWorkList.empty()) {
1009 BasicBlock *BB = BBWorkList.back();
1010 BBWorkList.pop_back();
Misha Brukmanb1c93172005-04-21 23:48:37 +00001011
Chris Lattner074be1f2004-11-15 04:44:20 +00001012 DEBUG(std::cerr << "\nPopped off BBWL: " << *BB);
Misha Brukmanb1c93172005-04-21 23:48:37 +00001013
Chris Lattner074be1f2004-11-15 04:44:20 +00001014 // Notify all instructions in this basic block that they are newly
1015 // executable.
1016 visit(BB);
1017 }
1018 }
1019}
1020
Chris Lattner7285f432004-12-10 20:41:50 +00001021/// ResolveBranchesIn - While solving the dataflow for a function, we assume
1022/// that branches on undef values cannot reach any of their successors.
1023/// However, this is not a safe assumption. After we solve dataflow, this
1024/// method should be use to handle this. If this returns true, the solver
1025/// should be rerun.
Chris Lattneraf170962006-10-22 05:59:17 +00001026///
1027/// This method handles this by finding an unresolved branch and marking it one
1028/// of the edges from the block as being feasible, even though the condition
1029/// doesn't say it would otherwise be. This allows SCCP to find the rest of the
1030/// CFG and only slightly pessimizes the analysis results (by marking one,
1031/// potentially unfeasible, edge feasible). This cannot usefully modify the
1032/// constraints on the condition of the branch, as that would impact other users
1033/// of the value.
Chris Lattner7285f432004-12-10 20:41:50 +00001034bool SCCPSolver::ResolveBranchesIn(Function &F) {
Chris Lattneraf170962006-10-22 05:59:17 +00001035 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
1036 if (!BBExecutable.count(BB))
1037 continue;
1038
1039 TerminatorInst *TI = BB->getTerminator();
1040 if (BranchInst *BI = dyn_cast<BranchInst>(TI)) {
1041 if (!BI->isConditional()) continue;
1042 if (!getValueState(BI->getCondition()).isUndefined())
1043 continue;
1044 } else if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
1045 if (!getValueState(SI->getCondition()).isUndefined())
1046 continue;
1047 } else {
1048 continue;
Chris Lattner7285f432004-12-10 20:41:50 +00001049 }
Chris Lattneraf170962006-10-22 05:59:17 +00001050
1051 // If the edge to the first successor isn't thought to be feasible yet, mark
1052 // it so now.
1053 if (KnownFeasibleEdges.count(Edge(BB, TI->getSuccessor(0))))
1054 continue;
1055
1056 // Otherwise, it isn't already thought to be feasible. Mark it as such now
1057 // and return. This will make other blocks reachable, which will allow new
1058 // values to be discovered and existing ones to be moved in the lattice.
1059 markEdgeExecutable(BB, TI->getSuccessor(0));
1060 return true;
1061 }
Chris Lattner2f687fd2004-12-11 06:05:53 +00001062
Chris Lattneraf170962006-10-22 05:59:17 +00001063 return false;
Chris Lattner7285f432004-12-10 20:41:50 +00001064}
1065
Chris Lattner074be1f2004-11-15 04:44:20 +00001066
1067namespace {
Chris Lattnerb4394642004-12-10 08:02:06 +00001068 Statistic<> NumInstRemoved("sccp", "Number of instructions removed");
1069 Statistic<> NumDeadBlocks ("sccp", "Number of basic blocks unreachable");
1070
Chris Lattner1890f942004-11-15 07:15:04 +00001071 //===--------------------------------------------------------------------===//
Chris Lattner074be1f2004-11-15 04:44:20 +00001072 //
Chris Lattner1890f942004-11-15 07:15:04 +00001073 /// SCCP Class - This class uses the SCCPSolver to implement a per-function
1074 /// Sparse Conditional COnstant Propagator.
1075 ///
1076 struct SCCP : public FunctionPass {
1077 // runOnFunction - Run the Sparse Conditional Constant Propagation
1078 // algorithm, and return true if the function was modified.
1079 //
1080 bool runOnFunction(Function &F);
Misha Brukmanb1c93172005-04-21 23:48:37 +00001081
Chris Lattner1890f942004-11-15 07:15:04 +00001082 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
1083 AU.setPreservesCFG();
1084 }
1085 };
Chris Lattner074be1f2004-11-15 04:44:20 +00001086
Chris Lattnerc2d3d312006-08-27 22:42:52 +00001087 RegisterPass<SCCP> X("sccp", "Sparse Conditional Constant Propagation");
Chris Lattner074be1f2004-11-15 04:44:20 +00001088} // end anonymous namespace
1089
1090
1091// createSCCPPass - This is the public interface to this file...
1092FunctionPass *llvm::createSCCPPass() {
1093 return new SCCP();
1094}
1095
1096
Chris Lattner074be1f2004-11-15 04:44:20 +00001097// runOnFunction() - Run the Sparse Conditional Constant Propagation algorithm,
1098// and return true if the function was modified.
1099//
1100bool SCCP::runOnFunction(Function &F) {
Chris Lattnerd18c16b2004-11-15 05:45:33 +00001101 DEBUG(std::cerr << "SCCP on function '" << F.getName() << "'\n");
Chris Lattner074be1f2004-11-15 04:44:20 +00001102 SCCPSolver Solver;
1103
1104 // Mark the first block of the function as being executable.
1105 Solver.MarkBlockExecutable(F.begin());
1106
Chris Lattnerd18c16b2004-11-15 05:45:33 +00001107 // Mark all arguments to the function as being overdefined.
1108 hash_map<Value*, LatticeVal> &Values = Solver.getValueMapping();
Chris Lattner531f9e92005-03-15 04:54:21 +00001109 for (Function::arg_iterator AI = F.arg_begin(), E = F.arg_end(); AI != E; ++AI)
Chris Lattnerd18c16b2004-11-15 05:45:33 +00001110 Values[AI].markOverdefined();
1111
Chris Lattner074be1f2004-11-15 04:44:20 +00001112 // Solve for constants.
Chris Lattner7285f432004-12-10 20:41:50 +00001113 bool ResolvedBranches = true;
1114 while (ResolvedBranches) {
1115 Solver.Solve();
Chris Lattner2f687fd2004-12-11 06:05:53 +00001116 DEBUG(std::cerr << "RESOLVING UNDEF BRANCHES\n");
Chris Lattner7285f432004-12-10 20:41:50 +00001117 ResolvedBranches = Solver.ResolveBranchesIn(F);
1118 }
Chris Lattner074be1f2004-11-15 04:44:20 +00001119
Chris Lattnerd18c16b2004-11-15 05:45:33 +00001120 bool MadeChanges = false;
1121
1122 // If we decided that there are basic blocks that are dead in this function,
1123 // delete their contents now. Note that we cannot actually delete the blocks,
1124 // as we cannot modify the CFG of the function.
1125 //
1126 std::set<BasicBlock*> &ExecutableBBs = Solver.getExecutableBlocks();
1127 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
1128 if (!ExecutableBBs.count(BB)) {
1129 DEBUG(std::cerr << " BasicBlock Dead:" << *BB);
Chris Lattner9a038a32004-11-15 07:02:42 +00001130 ++NumDeadBlocks;
1131
Chris Lattnerd18c16b2004-11-15 05:45:33 +00001132 // Delete the instructions backwards, as it has a reduced likelihood of
1133 // having to update as many def-use and use-def chains.
1134 std::vector<Instruction*> Insts;
1135 for (BasicBlock::iterator I = BB->begin(), E = BB->getTerminator();
1136 I != E; ++I)
1137 Insts.push_back(I);
1138 while (!Insts.empty()) {
1139 Instruction *I = Insts.back();
1140 Insts.pop_back();
1141 if (!I->use_empty())
1142 I->replaceAllUsesWith(UndefValue::get(I->getType()));
1143 BB->getInstList().erase(I);
1144 MadeChanges = true;
Chris Lattner9a038a32004-11-15 07:02:42 +00001145 ++NumInstRemoved;
Chris Lattnerd18c16b2004-11-15 05:45:33 +00001146 }
Chris Lattnerb4394642004-12-10 08:02:06 +00001147 } else {
1148 // Iterate over all of the instructions in a function, replacing them with
1149 // constants if we have found them to be of constant values.
1150 //
1151 for (BasicBlock::iterator BI = BB->begin(), E = BB->end(); BI != E; ) {
1152 Instruction *Inst = BI++;
1153 if (Inst->getType() != Type::VoidTy) {
1154 LatticeVal &IV = Values[Inst];
1155 if (IV.isConstant() || IV.isUndefined() &&
1156 !isa<TerminatorInst>(Inst)) {
1157 Constant *Const = IV.isConstant()
1158 ? IV.getConstant() : UndefValue::get(Inst->getType());
Chris Lattner074be1f2004-11-15 04:44:20 +00001159 DEBUG(std::cerr << " Constant: " << *Const << " = " << *Inst);
Misha Brukmanb1c93172005-04-21 23:48:37 +00001160
Chris Lattnerb4394642004-12-10 08:02:06 +00001161 // Replaces all of the uses of a variable with uses of the constant.
1162 Inst->replaceAllUsesWith(Const);
Misha Brukmanb1c93172005-04-21 23:48:37 +00001163
Chris Lattnerb4394642004-12-10 08:02:06 +00001164 // Delete the instruction.
1165 BB->getInstList().erase(Inst);
Misha Brukmanb1c93172005-04-21 23:48:37 +00001166
Chris Lattnerb4394642004-12-10 08:02:06 +00001167 // Hey, we just changed something!
1168 MadeChanges = true;
1169 ++NumInstRemoved;
Chris Lattner074be1f2004-11-15 04:44:20 +00001170 }
Chris Lattner074be1f2004-11-15 04:44:20 +00001171 }
1172 }
1173 }
1174
1175 return MadeChanges;
1176}
Chris Lattnerb4394642004-12-10 08:02:06 +00001177
1178namespace {
1179 Statistic<> IPNumInstRemoved("ipsccp", "Number of instructions removed");
1180 Statistic<> IPNumDeadBlocks ("ipsccp", "Number of basic blocks unreachable");
1181 Statistic<> IPNumArgsElimed ("ipsccp",
1182 "Number of arguments constant propagated");
Chris Lattner91dbae62004-12-11 05:15:59 +00001183 Statistic<> IPNumGlobalConst("ipsccp",
1184 "Number of globals found to be constant");
Chris Lattnerb4394642004-12-10 08:02:06 +00001185
1186 //===--------------------------------------------------------------------===//
1187 //
1188 /// IPSCCP Class - This class implements interprocedural Sparse Conditional
1189 /// Constant Propagation.
1190 ///
1191 struct IPSCCP : public ModulePass {
1192 bool runOnModule(Module &M);
1193 };
1194
Chris Lattnerc2d3d312006-08-27 22:42:52 +00001195 RegisterPass<IPSCCP>
Chris Lattnerb4394642004-12-10 08:02:06 +00001196 Y("ipsccp", "Interprocedural Sparse Conditional Constant Propagation");
1197} // end anonymous namespace
1198
1199// createIPSCCPPass - This is the public interface to this file...
1200ModulePass *llvm::createIPSCCPPass() {
1201 return new IPSCCP();
1202}
1203
1204
1205static bool AddressIsTaken(GlobalValue *GV) {
Chris Lattner8cb10a12005-04-19 19:16:19 +00001206 // Delete any dead constantexpr klingons.
1207 GV->removeDeadConstantUsers();
1208
Chris Lattnerb4394642004-12-10 08:02:06 +00001209 for (Value::use_iterator UI = GV->use_begin(), E = GV->use_end();
1210 UI != E; ++UI)
1211 if (StoreInst *SI = dyn_cast<StoreInst>(*UI)) {
Chris Lattner91dbae62004-12-11 05:15:59 +00001212 if (SI->getOperand(0) == GV || SI->isVolatile())
1213 return true; // Storing addr of GV.
Chris Lattnerb4394642004-12-10 08:02:06 +00001214 } else if (isa<InvokeInst>(*UI) || isa<CallInst>(*UI)) {
1215 // Make sure we are calling the function, not passing the address.
1216 CallSite CS = CallSite::get(cast<Instruction>(*UI));
1217 for (CallSite::arg_iterator AI = CS.arg_begin(),
1218 E = CS.arg_end(); AI != E; ++AI)
1219 if (*AI == GV)
1220 return true;
Chris Lattner91dbae62004-12-11 05:15:59 +00001221 } else if (LoadInst *LI = dyn_cast<LoadInst>(*UI)) {
1222 if (LI->isVolatile())
1223 return true;
1224 } else {
Chris Lattnerb4394642004-12-10 08:02:06 +00001225 return true;
1226 }
1227 return false;
1228}
1229
1230bool IPSCCP::runOnModule(Module &M) {
1231 SCCPSolver Solver;
1232
1233 // Loop over all functions, marking arguments to those with their addresses
1234 // taken or that are external as overdefined.
1235 //
1236 hash_map<Value*, LatticeVal> &Values = Solver.getValueMapping();
1237 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F)
1238 if (!F->hasInternalLinkage() || AddressIsTaken(F)) {
1239 if (!F->isExternal())
1240 Solver.MarkBlockExecutable(F->begin());
Chris Lattner8cb10a12005-04-19 19:16:19 +00001241 for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end();
1242 AI != E; ++AI)
Chris Lattnerb4394642004-12-10 08:02:06 +00001243 Values[AI].markOverdefined();
1244 } else {
1245 Solver.AddTrackedFunction(F);
1246 }
1247
Chris Lattner91dbae62004-12-11 05:15:59 +00001248 // Loop over global variables. We inform the solver about any internal global
1249 // variables that do not have their 'addresses taken'. If they don't have
1250 // their addresses taken, we can propagate constants through them.
Chris Lattner8cb10a12005-04-19 19:16:19 +00001251 for (Module::global_iterator G = M.global_begin(), E = M.global_end();
1252 G != E; ++G)
Chris Lattner91dbae62004-12-11 05:15:59 +00001253 if (!G->isConstant() && G->hasInternalLinkage() && !AddressIsTaken(G))
1254 Solver.TrackValueOfGlobalVariable(G);
1255
Chris Lattnerb4394642004-12-10 08:02:06 +00001256 // Solve for constants.
Chris Lattner7285f432004-12-10 20:41:50 +00001257 bool ResolvedBranches = true;
1258 while (ResolvedBranches) {
1259 Solver.Solve();
1260
Chris Lattner2f687fd2004-12-11 06:05:53 +00001261 DEBUG(std::cerr << "RESOLVING UNDEF BRANCHES\n");
Chris Lattner7285f432004-12-10 20:41:50 +00001262 ResolvedBranches = false;
1263 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F)
1264 ResolvedBranches |= Solver.ResolveBranchesIn(*F);
1265 }
Chris Lattnerb4394642004-12-10 08:02:06 +00001266
1267 bool MadeChanges = false;
1268
1269 // Iterate over all of the instructions in the module, replacing them with
1270 // constants if we have found them to be of constant values.
1271 //
1272 std::set<BasicBlock*> &ExecutableBBs = Solver.getExecutableBlocks();
1273 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
Chris Lattner8cb10a12005-04-19 19:16:19 +00001274 for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end();
1275 AI != E; ++AI)
Chris Lattnerb4394642004-12-10 08:02:06 +00001276 if (!AI->use_empty()) {
1277 LatticeVal &IV = Values[AI];
1278 if (IV.isConstant() || IV.isUndefined()) {
1279 Constant *CST = IV.isConstant() ?
1280 IV.getConstant() : UndefValue::get(AI->getType());
1281 DEBUG(std::cerr << "*** Arg " << *AI << " = " << *CST <<"\n");
Misha Brukmanb1c93172005-04-21 23:48:37 +00001282
Chris Lattnerb4394642004-12-10 08:02:06 +00001283 // Replaces all of the uses of a variable with uses of the
1284 // constant.
1285 AI->replaceAllUsesWith(CST);
1286 ++IPNumArgsElimed;
1287 }
1288 }
1289
Chris Lattnerbae4b642004-12-10 22:29:08 +00001290 std::vector<BasicBlock*> BlocksToErase;
Chris Lattnerb4394642004-12-10 08:02:06 +00001291 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
1292 if (!ExecutableBBs.count(BB)) {
1293 DEBUG(std::cerr << " BasicBlock Dead:" << *BB);
1294 ++IPNumDeadBlocks;
Chris Lattner7285f432004-12-10 20:41:50 +00001295
Chris Lattnerb4394642004-12-10 08:02:06 +00001296 // Delete the instructions backwards, as it has a reduced likelihood of
1297 // having to update as many def-use and use-def chains.
1298 std::vector<Instruction*> Insts;
Chris Lattnerbae4b642004-12-10 22:29:08 +00001299 TerminatorInst *TI = BB->getTerminator();
1300 for (BasicBlock::iterator I = BB->begin(), E = TI; I != E; ++I)
Chris Lattnerb4394642004-12-10 08:02:06 +00001301 Insts.push_back(I);
Chris Lattnerbae4b642004-12-10 22:29:08 +00001302
Chris Lattnerb4394642004-12-10 08:02:06 +00001303 while (!Insts.empty()) {
1304 Instruction *I = Insts.back();
1305 Insts.pop_back();
1306 if (!I->use_empty())
1307 I->replaceAllUsesWith(UndefValue::get(I->getType()));
1308 BB->getInstList().erase(I);
1309 MadeChanges = true;
1310 ++IPNumInstRemoved;
1311 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001312
Chris Lattnerbae4b642004-12-10 22:29:08 +00001313 for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) {
1314 BasicBlock *Succ = TI->getSuccessor(i);
1315 if (Succ->begin() != Succ->end() && isa<PHINode>(Succ->begin()))
1316 TI->getSuccessor(i)->removePredecessor(BB);
1317 }
Chris Lattner99e12952004-12-11 02:53:57 +00001318 if (!TI->use_empty())
1319 TI->replaceAllUsesWith(UndefValue::get(TI->getType()));
Chris Lattnerbae4b642004-12-10 22:29:08 +00001320 BB->getInstList().erase(TI);
1321
Chris Lattner8525ebe2004-12-11 05:32:19 +00001322 if (&*BB != &F->front())
1323 BlocksToErase.push_back(BB);
1324 else
1325 new UnreachableInst(BB);
1326
Chris Lattnerb4394642004-12-10 08:02:06 +00001327 } else {
1328 for (BasicBlock::iterator BI = BB->begin(), E = BB->end(); BI != E; ) {
1329 Instruction *Inst = BI++;
1330 if (Inst->getType() != Type::VoidTy) {
1331 LatticeVal &IV = Values[Inst];
1332 if (IV.isConstant() || IV.isUndefined() &&
1333 !isa<TerminatorInst>(Inst)) {
1334 Constant *Const = IV.isConstant()
1335 ? IV.getConstant() : UndefValue::get(Inst->getType());
1336 DEBUG(std::cerr << " Constant: " << *Const << " = " << *Inst);
Misha Brukmanb1c93172005-04-21 23:48:37 +00001337
Chris Lattnerb4394642004-12-10 08:02:06 +00001338 // Replaces all of the uses of a variable with uses of the
1339 // constant.
1340 Inst->replaceAllUsesWith(Const);
Misha Brukmanb1c93172005-04-21 23:48:37 +00001341
Chris Lattnerb4394642004-12-10 08:02:06 +00001342 // Delete the instruction.
1343 if (!isa<TerminatorInst>(Inst) && !isa<CallInst>(Inst))
1344 BB->getInstList().erase(Inst);
1345
1346 // Hey, we just changed something!
1347 MadeChanges = true;
1348 ++IPNumInstRemoved;
1349 }
1350 }
1351 }
1352 }
Chris Lattnerbae4b642004-12-10 22:29:08 +00001353
1354 // Now that all instructions in the function are constant folded, erase dead
1355 // blocks, because we can now use ConstantFoldTerminator to get rid of
1356 // in-edges.
1357 for (unsigned i = 0, e = BlocksToErase.size(); i != e; ++i) {
1358 // If there are any PHI nodes in this successor, drop entries for BB now.
1359 BasicBlock *DeadBB = BlocksToErase[i];
1360 while (!DeadBB->use_empty()) {
1361 Instruction *I = cast<Instruction>(DeadBB->use_back());
1362 bool Folded = ConstantFoldTerminator(I->getParent());
Chris Lattnerfe7b6ef2006-10-23 18:57:02 +00001363 if (!Folded) {
1364 // The constant folder may not have been able to fold the termiantor
1365 // if this is a branch or switch on undef. Fold it manually as a
1366 // branch to the first successor.
1367 if (BranchInst *BI = dyn_cast<BranchInst>(I)) {
1368 assert(BI->isConditional() && isa<UndefValue>(BI->getCondition()) &&
1369 "Branch should be foldable!");
1370 } else if (SwitchInst *SI = dyn_cast<SwitchInst>(I)) {
1371 assert(isa<UndefValue>(SI->getCondition()) && "Switch should fold");
1372 } else {
1373 assert(0 && "Didn't fold away reference to block!");
1374 }
1375
1376 // Make this an uncond branch to the first successor.
1377 TerminatorInst *TI = I->getParent()->getTerminator();
1378 new BranchInst(TI->getSuccessor(0), TI);
1379
1380 // Remove entries in successor phi nodes to remove edges.
1381 for (unsigned i = 1, e = TI->getNumSuccessors(); i != e; ++i)
1382 TI->getSuccessor(i)->removePredecessor(TI->getParent());
1383
1384 // Remove the old terminator.
1385 TI->eraseFromParent();
1386 }
Chris Lattnerbae4b642004-12-10 22:29:08 +00001387 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001388
Chris Lattnerbae4b642004-12-10 22:29:08 +00001389 // Finally, delete the basic block.
1390 F->getBasicBlockList().erase(DeadBB);
1391 }
Chris Lattnerb4394642004-12-10 08:02:06 +00001392 }
Chris Lattner99e12952004-12-11 02:53:57 +00001393
1394 // If we inferred constant or undef return values for a function, we replaced
1395 // all call uses with the inferred value. This means we don't need to bother
1396 // actually returning anything from the function. Replace all return
1397 // instructions with return undef.
1398 const hash_map<Function*, LatticeVal> &RV =Solver.getTrackedFunctionRetVals();
1399 for (hash_map<Function*, LatticeVal>::const_iterator I = RV.begin(),
1400 E = RV.end(); I != E; ++I)
1401 if (!I->second.isOverdefined() &&
1402 I->first->getReturnType() != Type::VoidTy) {
1403 Function *F = I->first;
1404 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
1405 if (ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator()))
1406 if (!isa<UndefValue>(RI->getOperand(0)))
1407 RI->setOperand(0, UndefValue::get(F->getReturnType()));
1408 }
Chris Lattner91dbae62004-12-11 05:15:59 +00001409
1410 // If we infered constant or undef values for globals variables, we can delete
1411 // the global and any stores that remain to it.
1412 const hash_map<GlobalVariable*, LatticeVal> &TG = Solver.getTrackedGlobals();
1413 for (hash_map<GlobalVariable*, LatticeVal>::const_iterator I = TG.begin(),
1414 E = TG.end(); I != E; ++I) {
1415 GlobalVariable *GV = I->first;
1416 assert(!I->second.isOverdefined() &&
1417 "Overdefined values should have been taken out of the map!");
1418 DEBUG(std::cerr << "Found that GV '" << GV->getName()<< "' is constant!\n");
1419 while (!GV->use_empty()) {
1420 StoreInst *SI = cast<StoreInst>(GV->use_back());
1421 SI->eraseFromParent();
1422 }
1423 M.getGlobalList().erase(GV);
Chris Lattner2f687fd2004-12-11 06:05:53 +00001424 ++IPNumGlobalConst;
Chris Lattner91dbae62004-12-11 05:15:59 +00001425 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001426
Chris Lattnerb4394642004-12-10 08:02:06 +00001427 return MadeChanges;
1428}