blob: 38a403b1a5d0b3c51fcaa31157f351c23d83022c [file] [log] [blame]
Misha Brukman82c89b92003-05-20 21:01:22 +00001//===- SCCP.cpp - Sparse Conditional Constant Propagation -----------------===//
John Criswellb576c942003-10-20 19:43:21 +00002//
3// 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.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner138a1242001-06-27 23:38:11 +00009//
Misha Brukman82c89b92003-05-20 21:01:22 +000010// This file implements sparse conditional constant propagation and merging:
Chris Lattner138a1242001-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 Lattner2a88bb72002-08-30 23:39:00 +000016// * Proves conditional branches to be unconditional
Chris Lattner138a1242001-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 Lattner022103b2002-05-07 20:03:00 +000024#include "llvm/Transforms/Scalar.h"
Chris Lattnerb7a5d3e2004-01-12 17:43:40 +000025#include "llvm/Constants.h"
Chris Lattner79df7c02002-03-26 18:01:55 +000026#include "llvm/Function.h"
Chris Lattnerc6a4d6a2004-01-12 04:29:41 +000027#include "llvm/GlobalVariable.h"
Chris Lattner9de28282003-04-25 02:50:03 +000028#include "llvm/Instructions.h"
Chris Lattnerbd0ef772002-02-26 21:46:54 +000029#include "llvm/Pass.h"
Chris Lattnerb7a5d3e2004-01-12 17:43:40 +000030#include "llvm/Type.h"
Chris Lattner2a632552002-04-18 15:13:15 +000031#include "llvm/Support/InstVisitor.h"
Chris Lattner58b7b082004-04-13 19:43:54 +000032#include "llvm/Transforms/Utils/Local.h"
Chris Lattner6806f562003-08-01 22:15:03 +000033#include "Support/Debug.h"
Chris Lattnera92f6962002-10-01 22:38:41 +000034#include "Support/Statistic.h"
Chris Lattner6806f562003-08-01 22:15:03 +000035#include "Support/STLExtras.h"
Chris Lattner138a1242001-06-27 23:38:11 +000036#include <algorithm>
Chris Lattner138a1242001-06-27 23:38:11 +000037#include <set>
Chris Lattnerd7456022004-01-09 06:02:20 +000038using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000039
Chris Lattner138a1242001-06-27 23:38:11 +000040// InstVal class - This class represents the different lattice values that an
Chris Lattnerf57b8452002-04-27 06:56:12 +000041// instruction may occupy. It is a simple class with value semantics.
Chris Lattner138a1242001-06-27 23:38:11 +000042//
Chris Lattner0dbfc052002-04-29 21:26:08 +000043namespace {
Chris Lattnera92f6962002-10-01 22:38:41 +000044 Statistic<> NumInstRemoved("sccp", "Number of instructions removed");
45
Chris Lattner138a1242001-06-27 23:38:11 +000046class InstVal {
47 enum {
Chris Lattnere9bb2df2001-12-03 22:26:30 +000048 undefined, // This instruction has no known value
49 constant, // This instruction has a constant value
Chris Lattnere9bb2df2001-12-03 22:26:30 +000050 overdefined // This instruction has an unknown value
51 } LatticeValue; // The current lattice position
52 Constant *ConstantVal; // If Constant value, the current value
Chris Lattner138a1242001-06-27 23:38:11 +000053public:
Chris Lattnere9bb2df2001-12-03 22:26:30 +000054 inline InstVal() : LatticeValue(undefined), ConstantVal(0) {}
Chris Lattner138a1242001-06-27 23:38:11 +000055
56 // markOverdefined - Return true if this is a new status to be in...
57 inline bool markOverdefined() {
Chris Lattnere9bb2df2001-12-03 22:26:30 +000058 if (LatticeValue != overdefined) {
59 LatticeValue = overdefined;
Chris Lattner138a1242001-06-27 23:38:11 +000060 return true;
61 }
62 return false;
63 }
64
65 // markConstant - Return true if this is a new status for us...
Chris Lattnere9bb2df2001-12-03 22:26:30 +000066 inline bool markConstant(Constant *V) {
67 if (LatticeValue != constant) {
68 LatticeValue = constant;
Chris Lattner138a1242001-06-27 23:38:11 +000069 ConstantVal = V;
70 return true;
71 } else {
Chris Lattnerb70d82f2001-09-07 16:43:22 +000072 assert(ConstantVal == V && "Marking constant with different value");
Chris Lattner138a1242001-06-27 23:38:11 +000073 }
74 return false;
75 }
76
Chris Lattnere9bb2df2001-12-03 22:26:30 +000077 inline bool isUndefined() const { return LatticeValue == undefined; }
78 inline bool isConstant() const { return LatticeValue == constant; }
79 inline bool isOverdefined() const { return LatticeValue == overdefined; }
Chris Lattner138a1242001-06-27 23:38:11 +000080
Chris Lattner1daee8b2004-01-12 03:57:30 +000081 inline Constant *getConstant() const {
82 assert(isConstant() && "Cannot get the constant of a non-constant!");
83 return ConstantVal;
84 }
Chris Lattner138a1242001-06-27 23:38:11 +000085};
86
Chris Lattner0dbfc052002-04-29 21:26:08 +000087} // end anonymous namespace
Chris Lattner138a1242001-06-27 23:38:11 +000088
89
90//===----------------------------------------------------------------------===//
91// SCCP Class
92//
Misha Brukman82c89b92003-05-20 21:01:22 +000093// This class does all of the work of Sparse Conditional Constant Propagation.
Chris Lattner138a1242001-06-27 23:38:11 +000094//
Chris Lattner0dbfc052002-04-29 21:26:08 +000095namespace {
96class SCCP : public FunctionPass, public InstVisitor<SCCP> {
Chris Lattner697954c2002-01-20 22:54:45 +000097 std::set<BasicBlock*> BBExecutable;// The basic blocks that are executable
98 std::map<Value*, InstVal> ValueState; // The state each value is in...
Chris Lattner138a1242001-06-27 23:38:11 +000099
Chris Lattner071d0ad2002-05-07 04:29:32 +0000100 std::vector<Instruction*> InstWorkList;// The instruction work list
Chris Lattner697954c2002-01-20 22:54:45 +0000101 std::vector<BasicBlock*> BBWorkList; // The BasicBlock work list
Chris Lattner16b18fd2003-10-08 16:55:34 +0000102
Chris Lattner1daee8b2004-01-12 03:57:30 +0000103 /// UsersOfOverdefinedPHIs - Keep track of any users of PHI nodes that are not
104 /// overdefined, despite the fact that the PHI node is overdefined.
105 std::multimap<PHINode*, Instruction*> UsersOfOverdefinedPHIs;
106
Chris Lattner16b18fd2003-10-08 16:55:34 +0000107 /// KnownFeasibleEdges - Entries in this set are edges which have already had
108 /// PHI nodes retriggered.
109 typedef std::pair<BasicBlock*,BasicBlock*> Edge;
110 std::set<Edge> KnownFeasibleEdges;
Chris Lattner138a1242001-06-27 23:38:11 +0000111public:
112
Misha Brukman82c89b92003-05-20 21:01:22 +0000113 // runOnFunction - Run the Sparse Conditional Constant Propagation algorithm,
Chris Lattner0dbfc052002-04-29 21:26:08 +0000114 // and return true if the function was modified.
115 //
Chris Lattner7e708292002-06-25 16:13:24 +0000116 bool runOnFunction(Function &F);
Chris Lattner0dbfc052002-04-29 21:26:08 +0000117
118 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattnercb2610e2002-10-21 20:00:28 +0000119 AU.setPreservesCFG();
Chris Lattner0dbfc052002-04-29 21:26:08 +0000120 }
121
Chris Lattner138a1242001-06-27 23:38:11 +0000122
123 //===--------------------------------------------------------------------===//
124 // The implementation of this class
125 //
126private:
Chris Lattner2a632552002-04-18 15:13:15 +0000127 friend class InstVisitor<SCCP>; // Allow callbacks from visitor
Chris Lattner138a1242001-06-27 23:38:11 +0000128
129 // markValueOverdefined - Make a value be marked as "constant". If the value
130 // is not already a constant, add it to the instruction work list so that
131 // the users of the instruction are updated later.
132 //
Chris Lattner3d405b02003-10-08 16:21:03 +0000133 inline void markConstant(InstVal &IV, Instruction *I, Constant *C) {
134 if (IV.markConstant(C)) {
135 DEBUG(std::cerr << "markConstant: " << *C << ": " << *I);
Chris Lattner071d0ad2002-05-07 04:29:32 +0000136 InstWorkList.push_back(I);
Chris Lattner138a1242001-06-27 23:38:11 +0000137 }
Chris Lattner3d405b02003-10-08 16:21:03 +0000138 }
139 inline void markConstant(Instruction *I, Constant *C) {
140 markConstant(ValueState[I], I, C);
Chris Lattner138a1242001-06-27 23:38:11 +0000141 }
142
143 // markValueOverdefined - Make a value be marked as "overdefined". If the
144 // value is not already overdefined, add it to the instruction work list so
145 // that the users of the instruction are updated later.
146 //
Chris Lattner3d405b02003-10-08 16:21:03 +0000147 inline void markOverdefined(InstVal &IV, Instruction *I) {
148 if (IV.markOverdefined()) {
149 DEBUG(std::cerr << "markOverdefined: " << *I);
150 InstWorkList.push_back(I); // Only instructions go on the work list
Chris Lattner138a1242001-06-27 23:38:11 +0000151 }
Chris Lattner3d405b02003-10-08 16:21:03 +0000152 }
153 inline void markOverdefined(Instruction *I) {
154 markOverdefined(ValueState[I], I);
Chris Lattner138a1242001-06-27 23:38:11 +0000155 }
156
157 // getValueState - Return the InstVal object that corresponds to the value.
Misha Brukman5560c9d2003-08-18 14:43:39 +0000158 // This function is necessary because not all values should start out in the
Chris Lattner73e21422002-04-09 19:48:49 +0000159 // underdefined state... Argument's should be overdefined, and
Chris Lattner79df7c02002-03-26 18:01:55 +0000160 // constants should be marked as constants. If a value is not known to be an
Chris Lattner138a1242001-06-27 23:38:11 +0000161 // Instruction object, then use this accessor to get its value from the map.
162 //
163 inline InstVal &getValueState(Value *V) {
Chris Lattner697954c2002-01-20 22:54:45 +0000164 std::map<Value*, InstVal>::iterator I = ValueState.find(V);
Chris Lattner138a1242001-06-27 23:38:11 +0000165 if (I != ValueState.end()) return I->second; // Common case, in the map
166
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000167 if (Constant *CPV = dyn_cast<Constant>(V)) { // Constants are constant
Chris Lattner138a1242001-06-27 23:38:11 +0000168 ValueState[CPV].markConstant(CPV);
Chris Lattner73e21422002-04-09 19:48:49 +0000169 } else if (isa<Argument>(V)) { // Arguments are overdefined
Chris Lattner138a1242001-06-27 23:38:11 +0000170 ValueState[V].markOverdefined();
Chris Lattner2a88bb72002-08-30 23:39:00 +0000171 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
172 // The address of a global is a constant...
173 ValueState[V].markConstant(ConstantPointerRef::get(GV));
174 }
Chris Lattner138a1242001-06-27 23:38:11 +0000175 // All others are underdefined by default...
176 return ValueState[V];
177 }
178
Chris Lattner16b18fd2003-10-08 16:55:34 +0000179 // markEdgeExecutable - Mark a basic block as executable, adding it to the BB
Chris Lattner138a1242001-06-27 23:38:11 +0000180 // work list if it is not already executable...
181 //
Chris Lattner16b18fd2003-10-08 16:55:34 +0000182 void markEdgeExecutable(BasicBlock *Source, BasicBlock *Dest) {
183 if (!KnownFeasibleEdges.insert(Edge(Source, Dest)).second)
184 return; // This edge is already known to be executable!
185
186 if (BBExecutable.count(Dest)) {
187 DEBUG(std::cerr << "Marking Edge Executable: " << Source->getName()
188 << " -> " << Dest->getName() << "\n");
189
190 // The destination is already executable, but we just made an edge
Chris Lattner929c6fb2003-10-08 16:56:11 +0000191 // feasible that wasn't before. Revisit the PHI nodes in the block
192 // because they have potentially new operands.
Chris Lattner16b18fd2003-10-08 16:55:34 +0000193 for (BasicBlock::iterator I = Dest->begin();
Chris Lattner9de28282003-04-25 02:50:03 +0000194 PHINode *PN = dyn_cast<PHINode>(I); ++I)
Chris Lattnerbceb2b02003-04-25 03:35:10 +0000195 visitPHINode(*PN);
Chris Lattner9de28282003-04-25 02:50:03 +0000196
197 } else {
Chris Lattner16b18fd2003-10-08 16:55:34 +0000198 DEBUG(std::cerr << "Marking Block Executable: " << Dest->getName()<<"\n");
199 BBExecutable.insert(Dest); // Basic block is executable!
200 BBWorkList.push_back(Dest); // Add the block to the work list!
Chris Lattner9de28282003-04-25 02:50:03 +0000201 }
Chris Lattner138a1242001-06-27 23:38:11 +0000202 }
203
Chris Lattner138a1242001-06-27 23:38:11 +0000204
Chris Lattner2a632552002-04-18 15:13:15 +0000205 // visit implementations - Something changed in this instruction... Either an
Chris Lattnercb056de2001-06-29 23:56:23 +0000206 // operand made a transition, or the instruction is newly executable. Change
207 // the value type of I to reflect these changes if appropriate.
208 //
Chris Lattner7e708292002-06-25 16:13:24 +0000209 void visitPHINode(PHINode &I);
Chris Lattner2a632552002-04-18 15:13:15 +0000210
211 // Terminators
Chris Lattner7e708292002-06-25 16:13:24 +0000212 void visitReturnInst(ReturnInst &I) { /*does not have an effect*/ }
213 void visitTerminatorInst(TerminatorInst &TI);
Chris Lattner2a632552002-04-18 15:13:15 +0000214
Chris Lattnerb8047602002-08-14 17:53:45 +0000215 void visitCastInst(CastInst &I);
Chris Lattner6e323722004-03-12 05:52:44 +0000216 void visitSelectInst(SelectInst &I);
Chris Lattner7e708292002-06-25 16:13:24 +0000217 void visitBinaryOperator(Instruction &I);
218 void visitShiftInst(ShiftInst &I) { visitBinaryOperator(I); }
Chris Lattner2a632552002-04-18 15:13:15 +0000219
220 // Instructions that cannot be folded away...
Chris Lattner7e708292002-06-25 16:13:24 +0000221 void visitStoreInst (Instruction &I) { /*returns void*/ }
Chris Lattnerc6a4d6a2004-01-12 04:29:41 +0000222 void visitLoadInst (LoadInst &I);
Chris Lattner2a88bb72002-08-30 23:39:00 +0000223 void visitGetElementPtrInst(GetElementPtrInst &I);
Chris Lattner58b7b082004-04-13 19:43:54 +0000224 void visitCallInst (CallInst &I);
Chris Lattner99b28e62003-08-27 01:08:35 +0000225 void visitInvokeInst (TerminatorInst &I) {
Chris Lattner3d405b02003-10-08 16:21:03 +0000226 if (I.getType() != Type::VoidTy) markOverdefined(&I);
Chris Lattner99b28e62003-08-27 01:08:35 +0000227 visitTerminatorInst(I);
228 }
Chris Lattner36143fc2003-09-08 18:54:55 +0000229 void visitUnwindInst (TerminatorInst &I) { /*returns void*/ }
Chris Lattner7e708292002-06-25 16:13:24 +0000230 void visitAllocationInst(Instruction &I) { markOverdefined(&I); }
Chris Lattnercda965e2003-10-18 05:56:52 +0000231 void visitVANextInst (Instruction &I) { markOverdefined(&I); }
232 void visitVAArgInst (Instruction &I) { markOverdefined(&I); }
Chris Lattner7e708292002-06-25 16:13:24 +0000233 void visitFreeInst (Instruction &I) { /*returns void*/ }
Chris Lattner2a632552002-04-18 15:13:15 +0000234
Chris Lattner7e708292002-06-25 16:13:24 +0000235 void visitInstruction(Instruction &I) {
Chris Lattner2a632552002-04-18 15:13:15 +0000236 // If a new instruction is added to LLVM that we don't handle...
Chris Lattner9de28282003-04-25 02:50:03 +0000237 std::cerr << "SCCP: Don't know how to handle: " << I;
Chris Lattner7e708292002-06-25 16:13:24 +0000238 markOverdefined(&I); // Just in case
Chris Lattner2a632552002-04-18 15:13:15 +0000239 }
Chris Lattnercb056de2001-06-29 23:56:23 +0000240
Chris Lattnerb9a66342002-05-02 21:44:00 +0000241 // getFeasibleSuccessors - Return a vector of booleans to indicate which
242 // successors are reachable from a given terminator instruction.
243 //
Chris Lattner7e708292002-06-25 16:13:24 +0000244 void getFeasibleSuccessors(TerminatorInst &TI, std::vector<bool> &Succs);
Chris Lattnerb9a66342002-05-02 21:44:00 +0000245
Chris Lattner59f0ce22002-05-02 21:18:01 +0000246 // isEdgeFeasible - Return true if the control flow edge from the 'From' basic
247 // block to the 'To' basic block is currently feasible...
248 //
249 bool isEdgeFeasible(BasicBlock *From, BasicBlock *To);
250
Chris Lattnercb056de2001-06-29 23:56:23 +0000251 // OperandChangedState - This method is invoked on all of the users of an
252 // instruction that was just changed state somehow.... Based on this
253 // information, we need to update the specified user of this instruction.
254 //
Chris Lattner59f0ce22002-05-02 21:18:01 +0000255 void OperandChangedState(User *U) {
256 // Only instructions use other variable values!
Chris Lattner7e708292002-06-25 16:13:24 +0000257 Instruction &I = cast<Instruction>(*U);
Chris Lattner9de28282003-04-25 02:50:03 +0000258 if (BBExecutable.count(I.getParent())) // Inst is executable?
259 visit(I);
Chris Lattner59f0ce22002-05-02 21:18:01 +0000260 }
Chris Lattnercb056de2001-06-29 23:56:23 +0000261};
Chris Lattnerf6293092002-07-23 18:06:35 +0000262
Chris Lattner9de28282003-04-25 02:50:03 +0000263 RegisterOpt<SCCP> X("sccp", "Sparse Conditional Constant Propagation");
Chris Lattner0dbfc052002-04-29 21:26:08 +0000264} // end anonymous namespace
265
266
267// createSCCPPass - This is the public interface to this file...
Chris Lattnerd7456022004-01-09 06:02:20 +0000268Pass *llvm::createSCCPPass() {
Chris Lattner0dbfc052002-04-29 21:26:08 +0000269 return new SCCP();
270}
271
Chris Lattner138a1242001-06-27 23:38:11 +0000272
Chris Lattner138a1242001-06-27 23:38:11 +0000273//===----------------------------------------------------------------------===//
274// SCCP Class Implementation
275
276
Misha Brukman82c89b92003-05-20 21:01:22 +0000277// runOnFunction() - Run the Sparse Conditional Constant Propagation algorithm,
Chris Lattner0dbfc052002-04-29 21:26:08 +0000278// and return true if the function was modified.
Chris Lattner138a1242001-06-27 23:38:11 +0000279//
Chris Lattner7e708292002-06-25 16:13:24 +0000280bool SCCP::runOnFunction(Function &F) {
Chris Lattnerf57b8452002-04-27 06:56:12 +0000281 // Mark the first block of the function as being executable...
Chris Lattner16b18fd2003-10-08 16:55:34 +0000282 BBExecutable.insert(F.begin()); // Basic block is executable!
283 BBWorkList.push_back(F.begin()); // Add the block to the work list!
Chris Lattner138a1242001-06-27 23:38:11 +0000284
285 // Process the work lists until their are empty!
286 while (!BBWorkList.empty() || !InstWorkList.empty()) {
287 // Process the instruction work list...
288 while (!InstWorkList.empty()) {
Chris Lattner071d0ad2002-05-07 04:29:32 +0000289 Instruction *I = InstWorkList.back();
290 InstWorkList.pop_back();
Chris Lattner138a1242001-06-27 23:38:11 +0000291
Chris Lattner9de28282003-04-25 02:50:03 +0000292 DEBUG(std::cerr << "\nPopped off I-WL: " << I);
Chris Lattner138a1242001-06-27 23:38:11 +0000293
294 // "I" got into the work list because it either made the transition from
295 // bottom to constant, or to Overdefined.
296 //
297 // Update all of the users of this instruction's value...
298 //
299 for_each(I->use_begin(), I->use_end(),
300 bind_obj(this, &SCCP::OperandChangedState));
301 }
302
303 // Process the basic block work list...
304 while (!BBWorkList.empty()) {
305 BasicBlock *BB = BBWorkList.back();
306 BBWorkList.pop_back();
307
Chris Lattner9de28282003-04-25 02:50:03 +0000308 DEBUG(std::cerr << "\nPopped off BBWL: " << BB);
Chris Lattner138a1242001-06-27 23:38:11 +0000309
Chris Lattner2a632552002-04-18 15:13:15 +0000310 // Notify all instructions in this basic block that they are newly
311 // executable.
312 visit(BB);
Chris Lattner138a1242001-06-27 23:38:11 +0000313 }
314 }
315
Chris Lattnerf016ea42002-05-22 17:17:27 +0000316 if (DebugFlag) {
Chris Lattner7e708292002-06-25 16:13:24 +0000317 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
318 if (!BBExecutable.count(I))
Chris Lattner9de28282003-04-25 02:50:03 +0000319 std::cerr << "BasicBlock Dead:" << *I;
Chris Lattnerf016ea42002-05-22 17:17:27 +0000320 }
Chris Lattner138a1242001-06-27 23:38:11 +0000321
Chris Lattnerf57b8452002-04-27 06:56:12 +0000322 // Iterate over all of the instructions in a function, replacing them with
Chris Lattner138a1242001-06-27 23:38:11 +0000323 // constants if we have found them to be of constant values.
324 //
325 bool MadeChanges = false;
Chris Lattner7e708292002-06-25 16:13:24 +0000326 for (Function::iterator BB = F.begin(), BBE = F.end(); BB != BBE; ++BB)
Chris Lattner221d6882002-02-12 21:07:25 +0000327 for (BasicBlock::iterator BI = BB->begin(); BI != BB->end();) {
Chris Lattner7e708292002-06-25 16:13:24 +0000328 Instruction &Inst = *BI;
329 InstVal &IV = ValueState[&Inst];
Chris Lattner221d6882002-02-12 21:07:25 +0000330 if (IV.isConstant()) {
331 Constant *Const = IV.getConstant();
Chris Lattner9de28282003-04-25 02:50:03 +0000332 DEBUG(std::cerr << "Constant: " << Const << " = " << Inst);
Chris Lattner5b7d42b2001-11-26 18:57:38 +0000333
Chris Lattner221d6882002-02-12 21:07:25 +0000334 // Replaces all of the uses of a variable with uses of the constant.
Chris Lattner7e708292002-06-25 16:13:24 +0000335 Inst.replaceAllUsesWith(Const);
Chris Lattner138a1242001-06-27 23:38:11 +0000336
Chris Lattner0e9c5152002-05-02 20:32:51 +0000337 // Remove the operator from the list of definitions... and delete it.
Chris Lattner7e708292002-06-25 16:13:24 +0000338 BI = BB->getInstList().erase(BI);
Chris Lattner138a1242001-06-27 23:38:11 +0000339
Chris Lattner221d6882002-02-12 21:07:25 +0000340 // Hey, we just changed something!
341 MadeChanges = true;
Chris Lattner3dec1f22002-05-10 15:38:35 +0000342 ++NumInstRemoved;
Chris Lattnerb9a66342002-05-02 21:44:00 +0000343 } else {
344 ++BI;
Chris Lattner221d6882002-02-12 21:07:25 +0000345 }
Chris Lattner138a1242001-06-27 23:38:11 +0000346 }
Chris Lattner138a1242001-06-27 23:38:11 +0000347
Chris Lattner59f0ce22002-05-02 21:18:01 +0000348 // Reset state so that the next invocation will have empty data structures
Chris Lattner0dbfc052002-04-29 21:26:08 +0000349 BBExecutable.clear();
350 ValueState.clear();
Chris Lattneraf663462002-11-04 02:54:22 +0000351 std::vector<Instruction*>().swap(InstWorkList);
352 std::vector<BasicBlock*>().swap(BBWorkList);
Chris Lattner0dbfc052002-04-29 21:26:08 +0000353
Chris Lattnerb70d82f2001-09-07 16:43:22 +0000354 return MadeChanges;
Chris Lattner138a1242001-06-27 23:38:11 +0000355}
356
Chris Lattnerb9a66342002-05-02 21:44:00 +0000357
358// getFeasibleSuccessors - Return a vector of booleans to indicate which
359// successors are reachable from a given terminator instruction.
360//
Chris Lattner7e708292002-06-25 16:13:24 +0000361void SCCP::getFeasibleSuccessors(TerminatorInst &TI, std::vector<bool> &Succs) {
Chris Lattner9de28282003-04-25 02:50:03 +0000362 Succs.resize(TI.getNumSuccessors());
Chris Lattner7e708292002-06-25 16:13:24 +0000363 if (BranchInst *BI = dyn_cast<BranchInst>(&TI)) {
Chris Lattnerb9a66342002-05-02 21:44:00 +0000364 if (BI->isUnconditional()) {
365 Succs[0] = true;
366 } else {
367 InstVal &BCValue = getValueState(BI->getCondition());
Chris Lattner84831642004-01-12 17:40:36 +0000368 if (BCValue.isOverdefined() ||
369 (BCValue.isConstant() && !isa<ConstantBool>(BCValue.getConstant()))) {
370 // Overdefined condition variables, and branches on unfoldable constant
371 // conditions, mean the branch could go either way.
Chris Lattnerb9a66342002-05-02 21:44:00 +0000372 Succs[0] = Succs[1] = true;
373 } else if (BCValue.isConstant()) {
374 // Constant condition variables mean the branch can only go a single way
375 Succs[BCValue.getConstant() == ConstantBool::False] = true;
376 }
377 }
Chris Lattner7e708292002-06-25 16:13:24 +0000378 } else if (InvokeInst *II = dyn_cast<InvokeInst>(&TI)) {
Chris Lattnerb9a66342002-05-02 21:44:00 +0000379 // Invoke instructions successors are always executable.
380 Succs[0] = Succs[1] = true;
Chris Lattner7e708292002-06-25 16:13:24 +0000381 } else if (SwitchInst *SI = dyn_cast<SwitchInst>(&TI)) {
Chris Lattnerb9a66342002-05-02 21:44:00 +0000382 InstVal &SCValue = getValueState(SI->getCondition());
Chris Lattner84831642004-01-12 17:40:36 +0000383 if (SCValue.isOverdefined() || // Overdefined condition?
384 (SCValue.isConstant() && !isa<ConstantInt>(SCValue.getConstant()))) {
Chris Lattnerb9a66342002-05-02 21:44:00 +0000385 // All destinations are executable!
Chris Lattner7e708292002-06-25 16:13:24 +0000386 Succs.assign(TI.getNumSuccessors(), true);
Chris Lattnerb9a66342002-05-02 21:44:00 +0000387 } else if (SCValue.isConstant()) {
388 Constant *CPV = SCValue.getConstant();
389 // Make sure to skip the "default value" which isn't a value
390 for (unsigned i = 1, E = SI->getNumSuccessors(); i != E; ++i) {
391 if (SI->getSuccessorValue(i) == CPV) {// Found the right branch...
392 Succs[i] = true;
393 return;
394 }
395 }
396
397 // Constant value not equal to any of the branches... must execute
398 // default branch then...
399 Succs[0] = true;
400 }
401 } else {
Chris Lattner9de28282003-04-25 02:50:03 +0000402 std::cerr << "SCCP: Don't know how to handle: " << TI;
Chris Lattner7e708292002-06-25 16:13:24 +0000403 Succs.assign(TI.getNumSuccessors(), true);
Chris Lattnerb9a66342002-05-02 21:44:00 +0000404 }
405}
406
407
Chris Lattner59f0ce22002-05-02 21:18:01 +0000408// isEdgeFeasible - Return true if the control flow edge from the 'From' basic
409// block to the 'To' basic block is currently feasible...
410//
411bool SCCP::isEdgeFeasible(BasicBlock *From, BasicBlock *To) {
412 assert(BBExecutable.count(To) && "Dest should always be alive!");
413
414 // Make sure the source basic block is executable!!
415 if (!BBExecutable.count(From)) return false;
416
Chris Lattnerb9a66342002-05-02 21:44:00 +0000417 // Check to make sure this edge itself is actually feasible now...
Chris Lattner7d275f42003-10-08 15:47:41 +0000418 TerminatorInst *TI = From->getTerminator();
419 if (BranchInst *BI = dyn_cast<BranchInst>(TI)) {
420 if (BI->isUnconditional())
Chris Lattnerb9a66342002-05-02 21:44:00 +0000421 return true;
Chris Lattner7d275f42003-10-08 15:47:41 +0000422 else {
423 InstVal &BCValue = getValueState(BI->getCondition());
424 if (BCValue.isOverdefined()) {
425 // Overdefined condition variables mean the branch could go either way.
426 return true;
427 } else if (BCValue.isConstant()) {
Chris Lattner84831642004-01-12 17:40:36 +0000428 // Not branching on an evaluatable constant?
429 if (!isa<ConstantBool>(BCValue.getConstant())) return true;
430
Chris Lattner7d275f42003-10-08 15:47:41 +0000431 // Constant condition variables mean the branch can only go a single way
432 return BI->getSuccessor(BCValue.getConstant() ==
433 ConstantBool::False) == To;
434 }
435 return false;
436 }
437 } else if (InvokeInst *II = dyn_cast<InvokeInst>(TI)) {
438 // Invoke instructions successors are always executable.
439 return true;
440 } else if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
441 InstVal &SCValue = getValueState(SI->getCondition());
442 if (SCValue.isOverdefined()) { // Overdefined condition?
443 // All destinations are executable!
444 return true;
445 } else if (SCValue.isConstant()) {
446 Constant *CPV = SCValue.getConstant();
Chris Lattner84831642004-01-12 17:40:36 +0000447 if (!isa<ConstantInt>(CPV))
448 return true; // not a foldable constant?
449
Chris Lattner7d275f42003-10-08 15:47:41 +0000450 // Make sure to skip the "default value" which isn't a value
451 for (unsigned i = 1, E = SI->getNumSuccessors(); i != E; ++i)
452 if (SI->getSuccessorValue(i) == CPV) // Found the taken branch...
453 return SI->getSuccessor(i) == To;
454
455 // Constant value not equal to any of the branches... must execute
456 // default branch then...
457 return SI->getDefaultDest() == To;
458 }
459 return false;
460 } else {
461 std::cerr << "Unknown terminator instruction: " << *TI;
462 abort();
463 }
Chris Lattner59f0ce22002-05-02 21:18:01 +0000464}
Chris Lattner138a1242001-06-27 23:38:11 +0000465
Chris Lattner2a632552002-04-18 15:13:15 +0000466// visit Implementations - Something changed in this instruction... Either an
Chris Lattner138a1242001-06-27 23:38:11 +0000467// operand made a transition, or the instruction is newly executable. Change
468// the value type of I to reflect these changes if appropriate. This method
469// makes sure to do the following actions:
470//
471// 1. If a phi node merges two constants in, and has conflicting value coming
472// from different branches, or if the PHI node merges in an overdefined
473// value, then the PHI node becomes overdefined.
474// 2. If a phi node merges only constants in, and they all agree on value, the
475// PHI node becomes a constant value equal to that.
476// 3. If V <- x (op) y && isConstant(x) && isConstant(y) V = Constant
477// 4. If V <- x (op) y && (isOverdefined(x) || isOverdefined(y)) V = Overdefined
478// 5. If V <- MEM or V <- CALL or V <- (unknown) then V = Overdefined
479// 6. If a conditional branch has a value that is constant, make the selected
480// destination executable
481// 7. If a conditional branch has a value that is overdefined, make all
482// successors executable.
483//
Chris Lattner7e708292002-06-25 16:13:24 +0000484void SCCP::visitPHINode(PHINode &PN) {
Chris Lattner3d405b02003-10-08 16:21:03 +0000485 InstVal &PNIV = getValueState(&PN);
Chris Lattner1daee8b2004-01-12 03:57:30 +0000486 if (PNIV.isOverdefined()) {
487 // There may be instructions using this PHI node that are not overdefined
488 // themselves. If so, make sure that they know that the PHI node operand
489 // changed.
490 std::multimap<PHINode*, Instruction*>::iterator I, E;
491 tie(I, E) = UsersOfOverdefinedPHIs.equal_range(&PN);
492 if (I != E) {
493 std::vector<Instruction*> Users;
494 Users.reserve(std::distance(I, E));
495 for (; I != E; ++I) Users.push_back(I->second);
496 while (!Users.empty()) {
497 visit(Users.back());
498 Users.pop_back();
499 }
500 }
501 return; // Quick exit
502 }
Chris Lattner138a1242001-06-27 23:38:11 +0000503
Chris Lattnera2f652d2004-03-16 19:49:59 +0000504 // Super-extra-high-degree PHI nodes are unlikely to ever be marked constant,
505 // and slow us down a lot. Just mark them overdefined.
506 if (PN.getNumIncomingValues() > 64) {
507 markOverdefined(PNIV, &PN);
508 return;
509 }
510
Chris Lattner2a632552002-04-18 15:13:15 +0000511 // Look at all of the executable operands of the PHI node. If any of them
512 // are overdefined, the PHI becomes overdefined as well. If they are all
513 // constant, and they agree with each other, the PHI becomes the identical
514 // constant. If they are constant and don't agree, the PHI is overdefined.
515 // If there are no executable operands, the PHI remains undefined.
516 //
Chris Lattner9de28282003-04-25 02:50:03 +0000517 Constant *OperandVal = 0;
518 for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) {
519 InstVal &IV = getValueState(PN.getIncomingValue(i));
520 if (IV.isUndefined()) continue; // Doesn't influence PHI node.
Chris Lattner9de28282003-04-25 02:50:03 +0000521
Chris Lattner7e708292002-06-25 16:13:24 +0000522 if (isEdgeFeasible(PN.getIncomingBlock(i), PN.getParent())) {
Chris Lattner38b5ae42003-06-24 20:29:52 +0000523 if (IV.isOverdefined()) { // PHI node becomes overdefined!
Chris Lattner3d405b02003-10-08 16:21:03 +0000524 markOverdefined(PNIV, &PN);
Chris Lattner38b5ae42003-06-24 20:29:52 +0000525 return;
526 }
527
Chris Lattner9de28282003-04-25 02:50:03 +0000528 if (OperandVal == 0) { // Grab the first value...
529 OperandVal = IV.getConstant();
Chris Lattner2a632552002-04-18 15:13:15 +0000530 } else { // Another value is being merged in!
531 // There is already a reachable operand. If we conflict with it,
532 // then the PHI node becomes overdefined. If we agree with it, we
533 // can continue on.
Chris Lattner9de28282003-04-25 02:50:03 +0000534
Chris Lattner2a632552002-04-18 15:13:15 +0000535 // Check to see if there are two different constants merging...
Chris Lattner9de28282003-04-25 02:50:03 +0000536 if (IV.getConstant() != OperandVal) {
Chris Lattner2a632552002-04-18 15:13:15 +0000537 // Yes there is. This means the PHI node is not constant.
538 // You must be overdefined poor PHI.
539 //
Chris Lattner3d405b02003-10-08 16:21:03 +0000540 markOverdefined(PNIV, &PN); // The PHI node now becomes overdefined
Chris Lattner2a632552002-04-18 15:13:15 +0000541 return; // I'm done analyzing you
Chris Lattner5b7d42b2001-11-26 18:57:38 +0000542 }
Chris Lattner138a1242001-06-27 23:38:11 +0000543 }
544 }
Chris Lattner138a1242001-06-27 23:38:11 +0000545 }
546
Chris Lattner2a632552002-04-18 15:13:15 +0000547 // If we exited the loop, this means that the PHI node only has constant
Chris Lattner9de28282003-04-25 02:50:03 +0000548 // arguments that agree with each other(and OperandVal is the constant) or
549 // OperandVal is null because there are no defined incoming arguments. If
550 // this is the case, the PHI remains undefined.
Chris Lattner138a1242001-06-27 23:38:11 +0000551 //
Chris Lattner9de28282003-04-25 02:50:03 +0000552 if (OperandVal)
Misha Brukmancf00c4a2003-10-10 17:57:28 +0000553 markConstant(PNIV, &PN, OperandVal); // Acquire operand value
Chris Lattner138a1242001-06-27 23:38:11 +0000554}
555
Chris Lattner7e708292002-06-25 16:13:24 +0000556void SCCP::visitTerminatorInst(TerminatorInst &TI) {
Chris Lattner9de28282003-04-25 02:50:03 +0000557 std::vector<bool> SuccFeasible;
Chris Lattnerb9a66342002-05-02 21:44:00 +0000558 getFeasibleSuccessors(TI, SuccFeasible);
Chris Lattner138a1242001-06-27 23:38:11 +0000559
Chris Lattner16b18fd2003-10-08 16:55:34 +0000560 BasicBlock *BB = TI.getParent();
561
Chris Lattnerb9a66342002-05-02 21:44:00 +0000562 // Mark all feasible successors executable...
563 for (unsigned i = 0, e = SuccFeasible.size(); i != e; ++i)
Chris Lattner16b18fd2003-10-08 16:55:34 +0000564 if (SuccFeasible[i])
565 markEdgeExecutable(BB, TI.getSuccessor(i));
Chris Lattner2a632552002-04-18 15:13:15 +0000566}
567
Chris Lattnerb8047602002-08-14 17:53:45 +0000568void SCCP::visitCastInst(CastInst &I) {
Chris Lattner7e708292002-06-25 16:13:24 +0000569 Value *V = I.getOperand(0);
Chris Lattner2a632552002-04-18 15:13:15 +0000570 InstVal &VState = getValueState(V);
Chris Lattnerb7a5d3e2004-01-12 17:43:40 +0000571 if (VState.isOverdefined()) // Inherit overdefinedness of operand
Chris Lattner7e708292002-06-25 16:13:24 +0000572 markOverdefined(&I);
Chris Lattnerb7a5d3e2004-01-12 17:43:40 +0000573 else if (VState.isConstant()) // Propagate constant value
574 markConstant(&I, ConstantExpr::getCast(VState.getConstant(), I.getType()));
Chris Lattner2a632552002-04-18 15:13:15 +0000575}
576
Chris Lattner6e323722004-03-12 05:52:44 +0000577void SCCP::visitSelectInst(SelectInst &I) {
578 InstVal &CondValue = getValueState(I.getCondition());
579 if (CondValue.isOverdefined())
580 markOverdefined(&I);
581 else if (CondValue.isConstant()) {
582 if (CondValue.getConstant() == ConstantBool::True) {
583 InstVal &Val = getValueState(I.getTrueValue());
584 if (Val.isOverdefined())
585 markOverdefined(&I);
586 else if (Val.isConstant())
587 markConstant(&I, Val.getConstant());
588 } else if (CondValue.getConstant() == ConstantBool::False) {
589 InstVal &Val = getValueState(I.getFalseValue());
590 if (Val.isOverdefined())
591 markOverdefined(&I);
592 else if (Val.isConstant())
593 markConstant(&I, Val.getConstant());
594 } else
595 markOverdefined(&I);
596 }
597}
598
Chris Lattner2a632552002-04-18 15:13:15 +0000599// Handle BinaryOperators and Shift Instructions...
Chris Lattner7e708292002-06-25 16:13:24 +0000600void SCCP::visitBinaryOperator(Instruction &I) {
Chris Lattner1daee8b2004-01-12 03:57:30 +0000601 InstVal &IV = ValueState[&I];
602 if (IV.isOverdefined()) return;
603
Chris Lattner7e708292002-06-25 16:13:24 +0000604 InstVal &V1State = getValueState(I.getOperand(0));
605 InstVal &V2State = getValueState(I.getOperand(1));
Chris Lattner1daee8b2004-01-12 03:57:30 +0000606
Chris Lattner2a632552002-04-18 15:13:15 +0000607 if (V1State.isOverdefined() || V2State.isOverdefined()) {
Chris Lattner1daee8b2004-01-12 03:57:30 +0000608 // If both operands are PHI nodes, it is possible that this instruction has
609 // a constant value, despite the fact that the PHI node doesn't. Check for
610 // this condition now.
611 if (PHINode *PN1 = dyn_cast<PHINode>(I.getOperand(0)))
612 if (PHINode *PN2 = dyn_cast<PHINode>(I.getOperand(1)))
613 if (PN1->getParent() == PN2->getParent()) {
614 // Since the two PHI nodes are in the same basic block, they must have
615 // entries for the same predecessors. Walk the predecessor list, and
616 // if all of the incoming values are constants, and the result of
617 // evaluating this expression with all incoming value pairs is the
618 // same, then this expression is a constant even though the PHI node
619 // is not a constant!
620 InstVal Result;
621 for (unsigned i = 0, e = PN1->getNumIncomingValues(); i != e; ++i) {
622 InstVal &In1 = getValueState(PN1->getIncomingValue(i));
623 BasicBlock *InBlock = PN1->getIncomingBlock(i);
624 InstVal &In2 =getValueState(PN2->getIncomingValueForBlock(InBlock));
625
626 if (In1.isOverdefined() || In2.isOverdefined()) {
627 Result.markOverdefined();
628 break; // Cannot fold this operation over the PHI nodes!
629 } else if (In1.isConstant() && In2.isConstant()) {
Chris Lattnerb16689b2004-01-12 19:08:43 +0000630 Constant *V = ConstantExpr::get(I.getOpcode(), In1.getConstant(),
631 In2.getConstant());
Chris Lattner1daee8b2004-01-12 03:57:30 +0000632 if (Result.isUndefined())
Chris Lattnerb16689b2004-01-12 19:08:43 +0000633 Result.markConstant(V);
634 else if (Result.isConstant() && Result.getConstant() != V) {
Chris Lattner1daee8b2004-01-12 03:57:30 +0000635 Result.markOverdefined();
636 break;
637 }
638 }
639 }
640
641 // If we found a constant value here, then we know the instruction is
642 // constant despite the fact that the PHI nodes are overdefined.
643 if (Result.isConstant()) {
644 markConstant(IV, &I, Result.getConstant());
645 // Remember that this instruction is virtually using the PHI node
646 // operands.
647 UsersOfOverdefinedPHIs.insert(std::make_pair(PN1, &I));
648 UsersOfOverdefinedPHIs.insert(std::make_pair(PN2, &I));
649 return;
650 } else if (Result.isUndefined()) {
651 return;
652 }
653
654 // Okay, this really is overdefined now. Since we might have
655 // speculatively thought that this was not overdefined before, and
656 // added ourselves to the UsersOfOverdefinedPHIs list for the PHIs,
657 // make sure to clean out any entries that we put there, for
658 // efficiency.
659 std::multimap<PHINode*, Instruction*>::iterator It, E;
660 tie(It, E) = UsersOfOverdefinedPHIs.equal_range(PN1);
661 while (It != E) {
662 if (It->second == &I) {
663 UsersOfOverdefinedPHIs.erase(It++);
664 } else
665 ++It;
666 }
667 tie(It, E) = UsersOfOverdefinedPHIs.equal_range(PN2);
668 while (It != E) {
669 if (It->second == &I) {
670 UsersOfOverdefinedPHIs.erase(It++);
671 } else
672 ++It;
673 }
674 }
675
676 markOverdefined(IV, &I);
Chris Lattner2a632552002-04-18 15:13:15 +0000677 } else if (V1State.isConstant() && V2State.isConstant()) {
Chris Lattnerb16689b2004-01-12 19:08:43 +0000678 markConstant(IV, &I, ConstantExpr::get(I.getOpcode(), V1State.getConstant(),
679 V2State.getConstant()));
Chris Lattner2a632552002-04-18 15:13:15 +0000680 }
681}
Chris Lattner2a88bb72002-08-30 23:39:00 +0000682
683// Handle getelementptr instructions... if all operands are constants then we
684// can turn this into a getelementptr ConstantExpr.
685//
686void SCCP::visitGetElementPtrInst(GetElementPtrInst &I) {
Chris Lattnerc6a4d6a2004-01-12 04:29:41 +0000687 InstVal &IV = ValueState[&I];
688 if (IV.isOverdefined()) return;
689
Chris Lattner2a88bb72002-08-30 23:39:00 +0000690 std::vector<Constant*> Operands;
691 Operands.reserve(I.getNumOperands());
692
693 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) {
694 InstVal &State = getValueState(I.getOperand(i));
695 if (State.isUndefined())
696 return; // Operands are not resolved yet...
697 else if (State.isOverdefined()) {
Chris Lattnerc6a4d6a2004-01-12 04:29:41 +0000698 markOverdefined(IV, &I);
Chris Lattner2a88bb72002-08-30 23:39:00 +0000699 return;
700 }
701 assert(State.isConstant() && "Unknown state!");
702 Operands.push_back(State.getConstant());
703 }
704
705 Constant *Ptr = Operands[0];
706 Operands.erase(Operands.begin()); // Erase the pointer from idx list...
707
Chris Lattnerc6a4d6a2004-01-12 04:29:41 +0000708 markConstant(IV, &I, ConstantExpr::getGetElementPtr(Ptr, Operands));
Chris Lattner2a88bb72002-08-30 23:39:00 +0000709}
Brian Gaeked0fde302003-11-11 22:41:34 +0000710
Chris Lattnerc6a4d6a2004-01-12 04:29:41 +0000711/// GetGEPGlobalInitializer - Given a constant and a getelementptr constantexpr,
712/// return the constant value being addressed by the constant expression, or
713/// null if something is funny.
714///
715static Constant *GetGEPGlobalInitializer(Constant *C, ConstantExpr *CE) {
Chris Lattner28977af2004-04-05 01:30:19 +0000716 if (CE->getOperand(1) != Constant::getNullValue(CE->getOperand(1)->getType()))
Chris Lattnerc6a4d6a2004-01-12 04:29:41 +0000717 return 0; // Do not allow stepping over the value!
718
719 // Loop over all of the operands, tracking down which value we are
720 // addressing...
721 for (unsigned i = 2, e = CE->getNumOperands(); i != e; ++i)
722 if (ConstantUInt *CU = dyn_cast<ConstantUInt>(CE->getOperand(i))) {
Chris Lattnerde512b52004-02-15 05:55:15 +0000723 ConstantStruct *CS = dyn_cast<ConstantStruct>(C);
724 if (CS == 0) return 0;
Chris Lattnerc6a4d6a2004-01-12 04:29:41 +0000725 if (CU->getValue() >= CS->getValues().size()) return 0;
726 C = cast<Constant>(CS->getValues()[CU->getValue()]);
727 } else if (ConstantSInt *CS = dyn_cast<ConstantSInt>(CE->getOperand(i))) {
Chris Lattnerde512b52004-02-15 05:55:15 +0000728 ConstantArray *CA = dyn_cast<ConstantArray>(C);
729 if (CA == 0) return 0;
Chris Lattnerc6a4d6a2004-01-12 04:29:41 +0000730 if ((uint64_t)CS->getValue() >= CA->getValues().size()) return 0;
731 C = cast<Constant>(CA->getValues()[CS->getValue()]);
Chris Lattnerde512b52004-02-15 05:55:15 +0000732 } else
Chris Lattnerc6a4d6a2004-01-12 04:29:41 +0000733 return 0;
734 return C;
735}
736
737// Handle load instructions. If the operand is a constant pointer to a constant
738// global, we can replace the load with the loaded constant value!
739void SCCP::visitLoadInst(LoadInst &I) {
740 InstVal &IV = ValueState[&I];
741 if (IV.isOverdefined()) return;
742
743 InstVal &PtrVal = getValueState(I.getOperand(0));
744 if (PtrVal.isUndefined()) return; // The pointer is not resolved yet!
745 if (PtrVal.isConstant() && !I.isVolatile()) {
746 Value *Ptr = PtrVal.getConstant();
Chris Lattnerc76d8032004-03-07 22:16:24 +0000747 if (isa<ConstantPointerNull>(Ptr)) {
748 // load null -> null
749 markConstant(IV, &I, Constant::getNullValue(I.getType()));
750 return;
751 }
752
Chris Lattnerc6a4d6a2004-01-12 04:29:41 +0000753 if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(Ptr))
754 Ptr = CPR->getValue();
755
756 // Transform load (constant global) into the value loaded.
757 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Ptr))
758 if (GV->isConstant() && !GV->isExternal()) {
759 markConstant(IV, &I, GV->getInitializer());
760 return;
761 }
762
763 // Transform load (constantexpr_GEP global, 0, ...) into the value loaded.
764 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr))
765 if (CE->getOpcode() == Instruction::GetElementPtr)
766 if (ConstantPointerRef *G
767 = dyn_cast<ConstantPointerRef>(CE->getOperand(0)))
768 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(G->getValue()))
769 if (GV->isConstant() && !GV->isExternal())
770 if (Constant *V =
771 GetGEPGlobalInitializer(GV->getInitializer(), CE)) {
772 markConstant(IV, &I, V);
773 return;
774 }
775 }
776
777 // Otherwise we cannot say for certain what value this load will produce.
778 // Bail out.
779 markOverdefined(IV, &I);
780}
Chris Lattner58b7b082004-04-13 19:43:54 +0000781
782void SCCP::visitCallInst(CallInst &I) {
783 InstVal &IV = ValueState[&I];
784 if (IV.isOverdefined()) return;
785
786 Function *F = I.getCalledFunction();
787 if (F == 0 || !canConstantFoldCallTo(F)) {
788 markOverdefined(IV, &I);
789 return;
790 }
791
792 std::vector<Constant*> Operands;
793 Operands.reserve(I.getNumOperands()-1);
794
795 for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i) {
796 InstVal &State = getValueState(I.getOperand(i));
797 if (State.isUndefined())
798 return; // Operands are not resolved yet...
799 else if (State.isOverdefined()) {
800 markOverdefined(IV, &I);
801 return;
802 }
803 assert(State.isConstant() && "Unknown state!");
804 Operands.push_back(State.getConstant());
805 }
806
807 if (Constant *C = ConstantFoldCall(F, Operands))
808 markConstant(IV, &I, C);
809 else
810 markOverdefined(IV, &I);
811}