1 //===- SCCP.cpp - Sparse Conditional Constant Propogation -----------------===//
3 // This file implements sparse conditional constant propogation and merging:
6 // * Assumes values are constant unless proven otherwise
7 // * Assumes BasicBlocks are dead unless proven otherwise
8 // * Proves values to be constant, and replaces them with constants
9 // * Proves conditional branches constant, and unconditionalizes them
10 // * Folds multiple identical constants in the constant pool together
13 // * This pass has a habit of making definitions be dead. It is a good idea
14 // to to run a DCE pass sometime after running this pass.
16 //===----------------------------------------------------------------------===//
18 #include "llvm/Transforms/Scalar.h"
19 #include "llvm/ConstantHandling.h"
20 #include "llvm/Function.h"
21 #include "llvm/BasicBlock.h"
22 #include "llvm/iPHINode.h"
23 #include "llvm/iMemory.h"
24 #include "llvm/iTerminators.h"
25 #include "llvm/iOther.h"
26 #include "llvm/Pass.h"
27 #include "llvm/Support/InstVisitor.h"
28 #include "Support/STLExtras.h"
29 #include "Support/StatisticReporter.h"
35 static Statistic<> NumInstRemoved("sccp\t\t- Number of instructions removed");
37 // InstVal class - This class represents the different lattice values that an
38 // instruction may occupy. It is a simple class with value semantics.
43 undefined, // This instruction has no known value
44 constant, // This instruction has a constant value
45 // Range, // This instruction is known to fall within a range
46 overdefined // This instruction has an unknown value
47 } LatticeValue; // The current lattice position
48 Constant *ConstantVal; // If Constant value, the current value
50 inline InstVal() : LatticeValue(undefined), ConstantVal(0) {}
52 // markOverdefined - Return true if this is a new status to be in...
53 inline bool markOverdefined() {
54 if (LatticeValue != overdefined) {
55 LatticeValue = overdefined;
61 // markConstant - Return true if this is a new status for us...
62 inline bool markConstant(Constant *V) {
63 if (LatticeValue != constant) {
64 LatticeValue = constant;
68 assert(ConstantVal == V && "Marking constant with different value");
73 inline bool isUndefined() const { return LatticeValue == undefined; }
74 inline bool isConstant() const { return LatticeValue == constant; }
75 inline bool isOverdefined() const { return LatticeValue == overdefined; }
77 inline Constant *getConstant() const { return ConstantVal; }
80 } // end anonymous namespace
83 //===----------------------------------------------------------------------===//
86 // This class does all of the work of Sparse Conditional Constant Propogation.
89 class SCCP : public FunctionPass, public InstVisitor<SCCP> {
90 std::set<BasicBlock*> BBExecutable;// The basic blocks that are executable
91 std::map<Value*, InstVal> ValueState; // The state each value is in...
93 std::vector<Instruction*> InstWorkList;// The instruction work list
94 std::vector<BasicBlock*> BBWorkList; // The BasicBlock work list
97 // runOnFunction - Run the Sparse Conditional Constant Propogation algorithm,
98 // and return true if the function was modified.
100 bool runOnFunction(Function &F);
102 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
107 //===--------------------------------------------------------------------===//
108 // The implementation of this class
111 friend class InstVisitor<SCCP>; // Allow callbacks from visitor
113 // markValueOverdefined - Make a value be marked as "constant". If the value
114 // is not already a constant, add it to the instruction work list so that
115 // the users of the instruction are updated later.
117 inline bool markConstant(Instruction *I, Constant *V) {
118 DEBUG(cerr << "markConstant: " << V << " = " << I);
120 if (ValueState[I].markConstant(V)) {
121 InstWorkList.push_back(I);
127 // markValueOverdefined - Make a value be marked as "overdefined". If the
128 // value is not already overdefined, add it to the instruction work list so
129 // that the users of the instruction are updated later.
131 inline bool markOverdefined(Value *V) {
132 if (ValueState[V].markOverdefined()) {
133 if (Instruction *I = dyn_cast<Instruction>(V)) {
134 DEBUG(cerr << "markOverdefined: " << V);
135 InstWorkList.push_back(I); // Only instructions go on the work list
142 // getValueState - Return the InstVal object that corresponds to the value.
143 // This function is neccesary because not all values should start out in the
144 // underdefined state... Argument's should be overdefined, and
145 // constants should be marked as constants. If a value is not known to be an
146 // Instruction object, then use this accessor to get its value from the map.
148 inline InstVal &getValueState(Value *V) {
149 std::map<Value*, InstVal>::iterator I = ValueState.find(V);
150 if (I != ValueState.end()) return I->second; // Common case, in the map
152 if (Constant *CPV = dyn_cast<Constant>(V)) { // Constants are constant
153 ValueState[CPV].markConstant(CPV);
154 } else if (isa<Argument>(V)) { // Arguments are overdefined
155 ValueState[V].markOverdefined();
157 // All others are underdefined by default...
158 return ValueState[V];
161 // markExecutable - Mark a basic block as executable, adding it to the BB
162 // work list if it is not already executable...
164 void markExecutable(BasicBlock *BB) {
165 if (BBExecutable.count(BB)) return;
166 DEBUG(cerr << "Marking BB Executable: " << *BB);
167 BBExecutable.insert(BB); // Basic block is executable!
168 BBWorkList.push_back(BB); // Add the block to the work list!
172 // visit implementations - Something changed in this instruction... Either an
173 // operand made a transition, or the instruction is newly executable. Change
174 // the value type of I to reflect these changes if appropriate.
176 void visitPHINode(PHINode &I);
179 void visitReturnInst(ReturnInst &I) { /*does not have an effect*/ }
180 void visitTerminatorInst(TerminatorInst &TI);
182 void visitUnaryOperator(Instruction &I);
183 void visitCastInst(CastInst &I) { visitUnaryOperator(I); }
184 void visitBinaryOperator(Instruction &I);
185 void visitShiftInst(ShiftInst &I) { visitBinaryOperator(I); }
187 // Instructions that cannot be folded away...
188 void visitStoreInst (Instruction &I) { /*returns void*/ }
189 void visitMemAccessInst (Instruction &I) { markOverdefined(&I); }
190 void visitCallInst (Instruction &I) { markOverdefined(&I); }
191 void visitInvokeInst (Instruction &I) { markOverdefined(&I); }
192 void visitAllocationInst(Instruction &I) { markOverdefined(&I); }
193 void visitFreeInst (Instruction &I) { /*returns void*/ }
195 void visitInstruction(Instruction &I) {
196 // If a new instruction is added to LLVM that we don't handle...
197 cerr << "SCCP: Don't know how to handle: " << I;
198 markOverdefined(&I); // Just in case
201 // getFeasibleSuccessors - Return a vector of booleans to indicate which
202 // successors are reachable from a given terminator instruction.
204 void getFeasibleSuccessors(TerminatorInst &TI, std::vector<bool> &Succs);
206 // isEdgeFeasible - Return true if the control flow edge from the 'From' basic
207 // block to the 'To' basic block is currently feasible...
209 bool isEdgeFeasible(BasicBlock *From, BasicBlock *To);
211 // OperandChangedState - This method is invoked on all of the users of an
212 // instruction that was just changed state somehow.... Based on this
213 // information, we need to update the specified user of this instruction.
215 void OperandChangedState(User *U) {
216 // Only instructions use other variable values!
217 Instruction &I = cast<Instruction>(*U);
218 if (!BBExecutable.count(I.getParent())) return;// Inst not executable yet!
223 RegisterPass<SCCP> X("sccp", "Sparse Conditional Constant Propogation");
224 } // end anonymous namespace
227 // createSCCPPass - This is the public interface to this file...
229 Pass *createSCCPPass() {
234 //===----------------------------------------------------------------------===//
235 // SCCP Class Implementation
238 // runOnFunction() - Run the Sparse Conditional Constant Propogation algorithm,
239 // and return true if the function was modified.
241 bool SCCP::runOnFunction(Function &F) {
242 // Mark the first block of the function as being executable...
243 markExecutable(&F.front());
245 // Process the work lists until their are empty!
246 while (!BBWorkList.empty() || !InstWorkList.empty()) {
247 // Process the instruction work list...
248 while (!InstWorkList.empty()) {
249 Instruction *I = InstWorkList.back();
250 InstWorkList.pop_back();
252 DEBUG(cerr << "\nPopped off I-WL: " << I);
255 // "I" got into the work list because it either made the transition from
256 // bottom to constant, or to Overdefined.
258 // Update all of the users of this instruction's value...
260 for_each(I->use_begin(), I->use_end(),
261 bind_obj(this, &SCCP::OperandChangedState));
264 // Process the basic block work list...
265 while (!BBWorkList.empty()) {
266 BasicBlock *BB = BBWorkList.back();
267 BBWorkList.pop_back();
269 DEBUG(cerr << "\nPopped off BBWL: " << BB);
271 // If this block only has a single successor, mark it as executable as
272 // well... if not, terminate the do loop.
274 if (BB->getTerminator()->getNumSuccessors() == 1)
275 markExecutable(BB->getTerminator()->getSuccessor(0));
277 // Notify all instructions in this basic block that they are newly
284 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
285 if (!BBExecutable.count(I))
286 cerr << "BasicBlock Dead:" << *I;
289 // Iterate over all of the instructions in a function, replacing them with
290 // constants if we have found them to be of constant values.
292 bool MadeChanges = false;
293 for (Function::iterator BB = F.begin(), BBE = F.end(); BB != BBE; ++BB)
294 for (BasicBlock::iterator BI = BB->begin(); BI != BB->end();) {
295 Instruction &Inst = *BI;
296 InstVal &IV = ValueState[&Inst];
297 if (IV.isConstant()) {
298 Constant *Const = IV.getConstant();
299 DEBUG(cerr << "Constant: " << Const << " = " << Inst);
301 // Replaces all of the uses of a variable with uses of the constant.
302 Inst.replaceAllUsesWith(Const);
304 // Remove the operator from the list of definitions... and delete it.
305 BI = BB->getInstList().erase(BI);
307 // Hey, we just changed something!
315 // Reset state so that the next invocation will have empty data structures
316 BBExecutable.clear();
323 // getFeasibleSuccessors - Return a vector of booleans to indicate which
324 // successors are reachable from a given terminator instruction.
326 void SCCP::getFeasibleSuccessors(TerminatorInst &TI, std::vector<bool> &Succs) {
327 assert(Succs.size() == TI.getNumSuccessors() && "Succs vector wrong size!");
328 if (BranchInst *BI = dyn_cast<BranchInst>(&TI)) {
329 if (BI->isUnconditional()) {
332 InstVal &BCValue = getValueState(BI->getCondition());
333 if (BCValue.isOverdefined()) {
334 // Overdefined condition variables mean the branch could go either way.
335 Succs[0] = Succs[1] = true;
336 } else if (BCValue.isConstant()) {
337 // Constant condition variables mean the branch can only go a single way
338 Succs[BCValue.getConstant() == ConstantBool::False] = true;
341 } else if (InvokeInst *II = dyn_cast<InvokeInst>(&TI)) {
342 // Invoke instructions successors are always executable.
343 Succs[0] = Succs[1] = true;
344 } else if (SwitchInst *SI = dyn_cast<SwitchInst>(&TI)) {
345 InstVal &SCValue = getValueState(SI->getCondition());
346 if (SCValue.isOverdefined()) { // Overdefined condition?
347 // All destinations are executable!
348 Succs.assign(TI.getNumSuccessors(), true);
349 } else if (SCValue.isConstant()) {
350 Constant *CPV = SCValue.getConstant();
351 // Make sure to skip the "default value" which isn't a value
352 for (unsigned i = 1, E = SI->getNumSuccessors(); i != E; ++i) {
353 if (SI->getSuccessorValue(i) == CPV) {// Found the right branch...
359 // Constant value not equal to any of the branches... must execute
360 // default branch then...
364 cerr << "SCCP: Don't know how to handle: " << TI;
365 Succs.assign(TI.getNumSuccessors(), true);
370 // isEdgeFeasible - Return true if the control flow edge from the 'From' basic
371 // block to the 'To' basic block is currently feasible...
373 bool SCCP::isEdgeFeasible(BasicBlock *From, BasicBlock *To) {
374 assert(BBExecutable.count(To) && "Dest should always be alive!");
376 // Make sure the source basic block is executable!!
377 if (!BBExecutable.count(From)) return false;
379 // Check to make sure this edge itself is actually feasible now...
380 TerminatorInst *FT = From->getTerminator();
381 std::vector<bool> SuccFeasible(FT->getNumSuccessors());
382 getFeasibleSuccessors(*FT, SuccFeasible);
384 // Check all edges from From to To. If any are feasible, return true.
385 for (unsigned i = 0, e = SuccFeasible.size(); i != e; ++i)
386 if (FT->getSuccessor(i) == To && SuccFeasible[i])
389 // Otherwise, none of the edges are actually feasible at this time...
393 // visit Implementations - Something changed in this instruction... Either an
394 // operand made a transition, or the instruction is newly executable. Change
395 // the value type of I to reflect these changes if appropriate. This method
396 // makes sure to do the following actions:
398 // 1. If a phi node merges two constants in, and has conflicting value coming
399 // from different branches, or if the PHI node merges in an overdefined
400 // value, then the PHI node becomes overdefined.
401 // 2. If a phi node merges only constants in, and they all agree on value, the
402 // PHI node becomes a constant value equal to that.
403 // 3. If V <- x (op) y && isConstant(x) && isConstant(y) V = Constant
404 // 4. If V <- x (op) y && (isOverdefined(x) || isOverdefined(y)) V = Overdefined
405 // 5. If V <- MEM or V <- CALL or V <- (unknown) then V = Overdefined
406 // 6. If a conditional branch has a value that is constant, make the selected
407 // destination executable
408 // 7. If a conditional branch has a value that is overdefined, make all
409 // successors executable.
412 void SCCP::visitPHINode(PHINode &PN) {
413 unsigned NumValues = PN.getNumIncomingValues(), i;
414 InstVal *OperandIV = 0;
416 // Look at all of the executable operands of the PHI node. If any of them
417 // are overdefined, the PHI becomes overdefined as well. If they are all
418 // constant, and they agree with each other, the PHI becomes the identical
419 // constant. If they are constant and don't agree, the PHI is overdefined.
420 // If there are no executable operands, the PHI remains undefined.
422 for (i = 0; i < NumValues; ++i) {
423 if (isEdgeFeasible(PN.getIncomingBlock(i), PN.getParent())) {
424 InstVal &IV = getValueState(PN.getIncomingValue(i));
425 if (IV.isUndefined()) continue; // Doesn't influence PHI node.
426 if (IV.isOverdefined()) { // PHI node becomes overdefined!
427 markOverdefined(&PN);
431 if (OperandIV == 0) { // Grab the first value...
433 } else { // Another value is being merged in!
434 // There is already a reachable operand. If we conflict with it,
435 // then the PHI node becomes overdefined. If we agree with it, we
438 // Check to see if there are two different constants merging...
439 if (IV.getConstant() != OperandIV->getConstant()) {
440 // Yes there is. This means the PHI node is not constant.
441 // You must be overdefined poor PHI.
443 markOverdefined(&PN); // The PHI node now becomes overdefined
444 return; // I'm done analyzing you
450 // If we exited the loop, this means that the PHI node only has constant
451 // arguments that agree with each other(and OperandIV is a pointer to one
452 // of their InstVal's) or OperandIV is null because there are no defined
453 // incoming arguments. If this is the case, the PHI remains undefined.
456 assert(OperandIV->isConstant() && "Should only be here for constants!");
457 markConstant(&PN, OperandIV->getConstant()); // Aquire operand value
461 void SCCP::visitTerminatorInst(TerminatorInst &TI) {
462 std::vector<bool> SuccFeasible(TI.getNumSuccessors());
463 getFeasibleSuccessors(TI, SuccFeasible);
465 // Mark all feasible successors executable...
466 for (unsigned i = 0, e = SuccFeasible.size(); i != e; ++i)
467 if (SuccFeasible[i]) {
468 BasicBlock *Succ = TI.getSuccessor(i);
469 markExecutable(Succ);
471 // Visit all of the PHI nodes that merge values from this block...
472 // Because this edge may be new executable, and PHI nodes that used to be
473 // constant now may not be.
475 for (BasicBlock::iterator I = Succ->begin();
476 PHINode *PN = dyn_cast<PHINode>(&*I); ++I)
481 void SCCP::visitUnaryOperator(Instruction &I) {
482 Value *V = I.getOperand(0);
483 InstVal &VState = getValueState(V);
484 if (VState.isOverdefined()) { // Inherit overdefinedness of operand
486 } else if (VState.isConstant()) { // Propogate constant value
487 Constant *Result = isa<CastInst>(I)
488 ? ConstantFoldCastInstruction(VState.getConstant(), I.getType())
489 : ConstantFoldUnaryInstruction(I.getOpcode(), VState.getConstant());
492 // This instruction constant folds!
493 markConstant(&I, Result);
495 markOverdefined(&I); // Don't know how to fold this instruction. :(
500 // Handle BinaryOperators and Shift Instructions...
501 void SCCP::visitBinaryOperator(Instruction &I) {
502 InstVal &V1State = getValueState(I.getOperand(0));
503 InstVal &V2State = getValueState(I.getOperand(1));
504 if (V1State.isOverdefined() || V2State.isOverdefined()) {
506 } else if (V1State.isConstant() && V2State.isConstant()) {
507 Constant *Result = 0;
508 if (isa<BinaryOperator>(I))
509 Result = ConstantFoldBinaryInstruction(I.getOpcode(),
510 V1State.getConstant(),
511 V2State.getConstant());
512 else if (isa<ShiftInst>(I))
513 Result = ConstantFoldShiftInstruction(I.getOpcode(),
514 V1State.getConstant(),
515 V2State.getConstant());
517 markConstant(&I, Result); // This instruction constant folds!
519 markOverdefined(&I); // Don't know how to fold this instruction. :(