1 //===- SCCP.cpp - Sparse Conditional Constant Propagation -----------------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file implements sparse conditional constant propagation and merging:
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
16 // * Proves conditional branches to be unconditional
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.
22 //===----------------------------------------------------------------------===//
24 #include "llvm/Transforms/Scalar.h"
25 #include "llvm/ConstantHandling.h"
26 #include "llvm/Function.h"
27 #include "llvm/Instructions.h"
28 #include "llvm/Pass.h"
29 #include "llvm/Support/InstVisitor.h"
30 #include "Support/Debug.h"
31 #include "Support/Statistic.h"
32 #include "Support/STLExtras.h"
36 // InstVal class - This class represents the different lattice values that an
37 // instruction may occupy. It is a simple class with value semantics.
40 Statistic<> NumInstRemoved("sccp", "Number of instructions removed");
44 undefined, // This instruction has no known value
45 constant, // This instruction has a constant value
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 Propagation.
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
96 /// KnownFeasibleEdges - Entries in this set are edges which have already had
97 /// PHI nodes retriggered.
98 typedef std::pair<BasicBlock*,BasicBlock*> Edge;
99 std::set<Edge> KnownFeasibleEdges;
102 // runOnFunction - Run the Sparse Conditional Constant Propagation algorithm,
103 // and return true if the function was modified.
105 bool runOnFunction(Function &F);
107 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
108 AU.setPreservesCFG();
112 //===--------------------------------------------------------------------===//
113 // The implementation of this class
116 friend class InstVisitor<SCCP>; // Allow callbacks from visitor
118 // markValueOverdefined - Make a value be marked as "constant". If the value
119 // is not already a constant, add it to the instruction work list so that
120 // the users of the instruction are updated later.
122 inline void markConstant(InstVal &IV, Instruction *I, Constant *C) {
123 if (IV.markConstant(C)) {
124 DEBUG(std::cerr << "markConstant: " << *C << ": " << *I);
125 InstWorkList.push_back(I);
128 inline void markConstant(Instruction *I, Constant *C) {
129 markConstant(ValueState[I], I, C);
132 // markValueOverdefined - Make a value be marked as "overdefined". If the
133 // value is not already overdefined, add it to the instruction work list so
134 // that the users of the instruction are updated later.
136 inline void markOverdefined(InstVal &IV, Instruction *I) {
137 if (IV.markOverdefined()) {
138 DEBUG(std::cerr << "markOverdefined: " << *I);
139 InstWorkList.push_back(I); // Only instructions go on the work list
142 inline void markOverdefined(Instruction *I) {
143 markOverdefined(ValueState[I], I);
146 // getValueState - Return the InstVal object that corresponds to the value.
147 // This function is necessary because not all values should start out in the
148 // underdefined state... Argument's should be overdefined, and
149 // constants should be marked as constants. If a value is not known to be an
150 // Instruction object, then use this accessor to get its value from the map.
152 inline InstVal &getValueState(Value *V) {
153 std::map<Value*, InstVal>::iterator I = ValueState.find(V);
154 if (I != ValueState.end()) return I->second; // Common case, in the map
156 if (Constant *CPV = dyn_cast<Constant>(V)) { // Constants are constant
157 ValueState[CPV].markConstant(CPV);
158 } else if (isa<Argument>(V)) { // Arguments are overdefined
159 ValueState[V].markOverdefined();
160 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
161 // The address of a global is a constant...
162 ValueState[V].markConstant(ConstantPointerRef::get(GV));
164 // All others are underdefined by default...
165 return ValueState[V];
168 // markEdgeExecutable - Mark a basic block as executable, adding it to the BB
169 // work list if it is not already executable...
171 void markEdgeExecutable(BasicBlock *Source, BasicBlock *Dest) {
172 if (!KnownFeasibleEdges.insert(Edge(Source, Dest)).second)
173 return; // This edge is already known to be executable!
175 if (BBExecutable.count(Dest)) {
176 DEBUG(std::cerr << "Marking Edge Executable: " << Source->getName()
177 << " -> " << Dest->getName() << "\n");
179 // The destination is already executable, but we just made an edge
180 // feasible that wasn't before. Revisit the PHI nodes in the block
181 // because they have potentially new operands.
182 for (BasicBlock::iterator I = Dest->begin();
183 PHINode *PN = dyn_cast<PHINode>(I); ++I)
187 DEBUG(std::cerr << "Marking Block Executable: " << Dest->getName()<<"\n");
188 BBExecutable.insert(Dest); // Basic block is executable!
189 BBWorkList.push_back(Dest); // Add the block to the work list!
194 // visit implementations - Something changed in this instruction... Either an
195 // operand made a transition, or the instruction is newly executable. Change
196 // the value type of I to reflect these changes if appropriate.
198 void visitPHINode(PHINode &I);
201 void visitReturnInst(ReturnInst &I) { /*does not have an effect*/ }
202 void visitTerminatorInst(TerminatorInst &TI);
204 void visitCastInst(CastInst &I);
205 void visitBinaryOperator(Instruction &I);
206 void visitShiftInst(ShiftInst &I) { visitBinaryOperator(I); }
208 // Instructions that cannot be folded away...
209 void visitStoreInst (Instruction &I) { /*returns void*/ }
210 void visitLoadInst (Instruction &I) { markOverdefined(&I); }
211 void visitGetElementPtrInst(GetElementPtrInst &I);
212 void visitCallInst (Instruction &I) { markOverdefined(&I); }
213 void visitInvokeInst (TerminatorInst &I) {
214 if (I.getType() != Type::VoidTy) markOverdefined(&I);
215 visitTerminatorInst(I);
217 void visitUnwindInst (TerminatorInst &I) { /*returns void*/ }
218 void visitAllocationInst(Instruction &I) { markOverdefined(&I); }
219 void visitVANextInst (Instruction &I) { markOverdefined(&I); }
220 void visitVAArgInst (Instruction &I) { markOverdefined(&I); }
221 void visitFreeInst (Instruction &I) { /*returns void*/ }
223 void visitInstruction(Instruction &I) {
224 // If a new instruction is added to LLVM that we don't handle...
225 std::cerr << "SCCP: Don't know how to handle: " << I;
226 markOverdefined(&I); // Just in case
229 // getFeasibleSuccessors - Return a vector of booleans to indicate which
230 // successors are reachable from a given terminator instruction.
232 void getFeasibleSuccessors(TerminatorInst &TI, std::vector<bool> &Succs);
234 // isEdgeFeasible - Return true if the control flow edge from the 'From' basic
235 // block to the 'To' basic block is currently feasible...
237 bool isEdgeFeasible(BasicBlock *From, BasicBlock *To);
239 // OperandChangedState - This method is invoked on all of the users of an
240 // instruction that was just changed state somehow.... Based on this
241 // information, we need to update the specified user of this instruction.
243 void OperandChangedState(User *U) {
244 // Only instructions use other variable values!
245 Instruction &I = cast<Instruction>(*U);
246 if (BBExecutable.count(I.getParent())) // Inst is executable?
251 RegisterOpt<SCCP> X("sccp", "Sparse Conditional Constant Propagation");
252 } // end anonymous namespace
255 // createSCCPPass - This is the public interface to this file...
257 Pass *createSCCPPass() {
262 //===----------------------------------------------------------------------===//
263 // SCCP Class Implementation
266 // runOnFunction() - Run the Sparse Conditional Constant Propagation algorithm,
267 // and return true if the function was modified.
269 bool SCCP::runOnFunction(Function &F) {
270 // Mark the first block of the function as being executable...
271 BBExecutable.insert(F.begin()); // Basic block is executable!
272 BBWorkList.push_back(F.begin()); // Add the block to the work list!
274 // Process the work lists until their are empty!
275 while (!BBWorkList.empty() || !InstWorkList.empty()) {
276 // Process the instruction work list...
277 while (!InstWorkList.empty()) {
278 Instruction *I = InstWorkList.back();
279 InstWorkList.pop_back();
281 DEBUG(std::cerr << "\nPopped off I-WL: " << I);
283 // "I" got into the work list because it either made the transition from
284 // bottom to constant, or to Overdefined.
286 // Update all of the users of this instruction's value...
288 for_each(I->use_begin(), I->use_end(),
289 bind_obj(this, &SCCP::OperandChangedState));
292 // Process the basic block work list...
293 while (!BBWorkList.empty()) {
294 BasicBlock *BB = BBWorkList.back();
295 BBWorkList.pop_back();
297 DEBUG(std::cerr << "\nPopped off BBWL: " << BB);
299 // Notify all instructions in this basic block that they are newly
306 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
307 if (!BBExecutable.count(I))
308 std::cerr << "BasicBlock Dead:" << *I;
311 // Iterate over all of the instructions in a function, replacing them with
312 // constants if we have found them to be of constant values.
314 bool MadeChanges = false;
315 for (Function::iterator BB = F.begin(), BBE = F.end(); BB != BBE; ++BB)
316 for (BasicBlock::iterator BI = BB->begin(); BI != BB->end();) {
317 Instruction &Inst = *BI;
318 InstVal &IV = ValueState[&Inst];
319 if (IV.isConstant()) {
320 Constant *Const = IV.getConstant();
321 DEBUG(std::cerr << "Constant: " << Const << " = " << Inst);
323 // Replaces all of the uses of a variable with uses of the constant.
324 Inst.replaceAllUsesWith(Const);
326 // Remove the operator from the list of definitions... and delete it.
327 BI = BB->getInstList().erase(BI);
329 // Hey, we just changed something!
337 // Reset state so that the next invocation will have empty data structures
338 BBExecutable.clear();
340 std::vector<Instruction*>().swap(InstWorkList);
341 std::vector<BasicBlock*>().swap(BBWorkList);
347 // getFeasibleSuccessors - Return a vector of booleans to indicate which
348 // successors are reachable from a given terminator instruction.
350 void SCCP::getFeasibleSuccessors(TerminatorInst &TI, std::vector<bool> &Succs) {
351 Succs.resize(TI.getNumSuccessors());
352 if (BranchInst *BI = dyn_cast<BranchInst>(&TI)) {
353 if (BI->isUnconditional()) {
356 InstVal &BCValue = getValueState(BI->getCondition());
357 if (BCValue.isOverdefined()) {
358 // Overdefined condition variables mean the branch could go either way.
359 Succs[0] = Succs[1] = true;
360 } else if (BCValue.isConstant()) {
361 // Constant condition variables mean the branch can only go a single way
362 Succs[BCValue.getConstant() == ConstantBool::False] = true;
365 } else if (InvokeInst *II = dyn_cast<InvokeInst>(&TI)) {
366 // Invoke instructions successors are always executable.
367 Succs[0] = Succs[1] = true;
368 } else if (SwitchInst *SI = dyn_cast<SwitchInst>(&TI)) {
369 InstVal &SCValue = getValueState(SI->getCondition());
370 if (SCValue.isOverdefined()) { // Overdefined condition?
371 // All destinations are executable!
372 Succs.assign(TI.getNumSuccessors(), true);
373 } else if (SCValue.isConstant()) {
374 Constant *CPV = SCValue.getConstant();
375 // Make sure to skip the "default value" which isn't a value
376 for (unsigned i = 1, E = SI->getNumSuccessors(); i != E; ++i) {
377 if (SI->getSuccessorValue(i) == CPV) {// Found the right branch...
383 // Constant value not equal to any of the branches... must execute
384 // default branch then...
388 std::cerr << "SCCP: Don't know how to handle: " << TI;
389 Succs.assign(TI.getNumSuccessors(), true);
394 // isEdgeFeasible - Return true if the control flow edge from the 'From' basic
395 // block to the 'To' basic block is currently feasible...
397 bool SCCP::isEdgeFeasible(BasicBlock *From, BasicBlock *To) {
398 assert(BBExecutable.count(To) && "Dest should always be alive!");
400 // Make sure the source basic block is executable!!
401 if (!BBExecutable.count(From)) return false;
403 // Check to make sure this edge itself is actually feasible now...
404 TerminatorInst *TI = From->getTerminator();
405 if (BranchInst *BI = dyn_cast<BranchInst>(TI)) {
406 if (BI->isUnconditional())
409 InstVal &BCValue = getValueState(BI->getCondition());
410 if (BCValue.isOverdefined()) {
411 // Overdefined condition variables mean the branch could go either way.
413 } else if (BCValue.isConstant()) {
414 // Constant condition variables mean the branch can only go a single way
415 return BI->getSuccessor(BCValue.getConstant() ==
416 ConstantBool::False) == To;
420 } else if (InvokeInst *II = dyn_cast<InvokeInst>(TI)) {
421 // Invoke instructions successors are always executable.
423 } else if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
424 InstVal &SCValue = getValueState(SI->getCondition());
425 if (SCValue.isOverdefined()) { // Overdefined condition?
426 // All destinations are executable!
428 } else if (SCValue.isConstant()) {
429 Constant *CPV = SCValue.getConstant();
430 // Make sure to skip the "default value" which isn't a value
431 for (unsigned i = 1, E = SI->getNumSuccessors(); i != E; ++i)
432 if (SI->getSuccessorValue(i) == CPV) // Found the taken branch...
433 return SI->getSuccessor(i) == To;
435 // Constant value not equal to any of the branches... must execute
436 // default branch then...
437 return SI->getDefaultDest() == To;
441 std::cerr << "Unknown terminator instruction: " << *TI;
446 // visit Implementations - Something changed in this instruction... Either an
447 // operand made a transition, or the instruction is newly executable. Change
448 // the value type of I to reflect these changes if appropriate. This method
449 // makes sure to do the following actions:
451 // 1. If a phi node merges two constants in, and has conflicting value coming
452 // from different branches, or if the PHI node merges in an overdefined
453 // value, then the PHI node becomes overdefined.
454 // 2. If a phi node merges only constants in, and they all agree on value, the
455 // PHI node becomes a constant value equal to that.
456 // 3. If V <- x (op) y && isConstant(x) && isConstant(y) V = Constant
457 // 4. If V <- x (op) y && (isOverdefined(x) || isOverdefined(y)) V = Overdefined
458 // 5. If V <- MEM or V <- CALL or V <- (unknown) then V = Overdefined
459 // 6. If a conditional branch has a value that is constant, make the selected
460 // destination executable
461 // 7. If a conditional branch has a value that is overdefined, make all
462 // successors executable.
464 void SCCP::visitPHINode(PHINode &PN) {
465 InstVal &PNIV = getValueState(&PN);
466 if (PNIV.isOverdefined()) return; // Quick exit
468 // Look at all of the executable operands of the PHI node. If any of them
469 // are overdefined, the PHI becomes overdefined as well. If they are all
470 // constant, and they agree with each other, the PHI becomes the identical
471 // constant. If they are constant and don't agree, the PHI is overdefined.
472 // If there are no executable operands, the PHI remains undefined.
474 Constant *OperandVal = 0;
475 for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) {
476 InstVal &IV = getValueState(PN.getIncomingValue(i));
477 if (IV.isUndefined()) continue; // Doesn't influence PHI node.
479 if (isEdgeFeasible(PN.getIncomingBlock(i), PN.getParent())) {
480 if (IV.isOverdefined()) { // PHI node becomes overdefined!
481 markOverdefined(PNIV, &PN);
485 if (OperandVal == 0) { // Grab the first value...
486 OperandVal = IV.getConstant();
487 } else { // Another value is being merged in!
488 // There is already a reachable operand. If we conflict with it,
489 // then the PHI node becomes overdefined. If we agree with it, we
492 // Check to see if there are two different constants merging...
493 if (IV.getConstant() != OperandVal) {
494 // Yes there is. This means the PHI node is not constant.
495 // You must be overdefined poor PHI.
497 markOverdefined(PNIV, &PN); // The PHI node now becomes overdefined
498 return; // I'm done analyzing you
504 // If we exited the loop, this means that the PHI node only has constant
505 // arguments that agree with each other(and OperandVal is the constant) or
506 // OperandVal is null because there are no defined incoming arguments. If
507 // this is the case, the PHI remains undefined.
510 markConstant(PNIV, &PN, OperandVal); // Acquire operand value
513 void SCCP::visitTerminatorInst(TerminatorInst &TI) {
514 std::vector<bool> SuccFeasible;
515 getFeasibleSuccessors(TI, SuccFeasible);
517 BasicBlock *BB = TI.getParent();
519 // Mark all feasible successors executable...
520 for (unsigned i = 0, e = SuccFeasible.size(); i != e; ++i)
522 markEdgeExecutable(BB, TI.getSuccessor(i));
525 void SCCP::visitCastInst(CastInst &I) {
526 Value *V = I.getOperand(0);
527 InstVal &VState = getValueState(V);
528 if (VState.isOverdefined()) { // Inherit overdefinedness of operand
530 } else if (VState.isConstant()) { // Propagate constant value
532 ConstantFoldCastInstruction(VState.getConstant(), I.getType());
534 if (Result) // If this instruction constant folds!
535 markConstant(&I, Result);
537 markOverdefined(&I); // Don't know how to fold this instruction. :(
541 // Handle BinaryOperators and Shift Instructions...
542 void SCCP::visitBinaryOperator(Instruction &I) {
543 InstVal &V1State = getValueState(I.getOperand(0));
544 InstVal &V2State = getValueState(I.getOperand(1));
545 if (V1State.isOverdefined() || V2State.isOverdefined()) {
547 } else if (V1State.isConstant() && V2State.isConstant()) {
548 Constant *Result = 0;
549 if (isa<BinaryOperator>(I))
550 Result = ConstantFoldBinaryInstruction(I.getOpcode(),
551 V1State.getConstant(),
552 V2State.getConstant());
553 else if (isa<ShiftInst>(I))
554 Result = ConstantFoldShiftInstruction(I.getOpcode(),
555 V1State.getConstant(),
556 V2State.getConstant());
558 markConstant(&I, Result); // This instruction constant folds!
560 markOverdefined(&I); // Don't know how to fold this instruction. :(
564 // Handle getelementptr instructions... if all operands are constants then we
565 // can turn this into a getelementptr ConstantExpr.
567 void SCCP::visitGetElementPtrInst(GetElementPtrInst &I) {
568 std::vector<Constant*> Operands;
569 Operands.reserve(I.getNumOperands());
571 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) {
572 InstVal &State = getValueState(I.getOperand(i));
573 if (State.isUndefined())
574 return; // Operands are not resolved yet...
575 else if (State.isOverdefined()) {
579 assert(State.isConstant() && "Unknown state!");
580 Operands.push_back(State.getConstant());
583 Constant *Ptr = Operands[0];
584 Operands.erase(Operands.begin()); // Erase the pointer from idx list...
586 markConstant(&I, ConstantExpr::getGetElementPtr(Ptr, Operands));