Chris Lattner | b0dbd7f | 2002-09-06 18:41:55 +0000 | [diff] [blame] | 1 | //===- CorrelatedExprs.cpp - Pass to detect and eliminated c.e.'s ---------===// |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 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. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | b0dbd7f | 2002-09-06 18:41:55 +0000 | [diff] [blame] | 9 | // |
Misha Brukman | a3bbcb5 | 2002-10-29 23:06:16 +0000 | [diff] [blame] | 10 | // Correlated Expression Elimination propagates information from conditional |
| 11 | // branches to blocks dominated by destinations of the branch. It propagates |
Chris Lattner | b0dbd7f | 2002-09-06 18:41:55 +0000 | [diff] [blame] | 12 | // information from the condition check itself into the body of the branch, |
| 13 | // allowing transformations like these for example: |
| 14 | // |
| 15 | // if (i == 7) |
Misha Brukman | a3bbcb5 | 2002-10-29 23:06:16 +0000 | [diff] [blame] | 16 | // ... 4*i; // constant propagation |
Chris Lattner | b0dbd7f | 2002-09-06 18:41:55 +0000 | [diff] [blame] | 17 | // |
| 18 | // M = i+1; N = j+1; |
| 19 | // if (i == j) |
| 20 | // X = M-N; // = M-M == 0; |
| 21 | // |
| 22 | // This is called Correlated Expression Elimination because we eliminate or |
| 23 | // simplify expressions that are correlated with the direction of a branch. In |
| 24 | // this way we use static information to give us some information about the |
| 25 | // dynamic value of a variable. |
| 26 | // |
| 27 | //===----------------------------------------------------------------------===// |
| 28 | |
| 29 | #include "llvm/Transforms/Scalar.h" |
Chris Lattner | 5585b33 | 2004-01-12 19:12:50 +0000 | [diff] [blame] | 30 | #include "llvm/Constants.h" |
Chris Lattner | b0dbd7f | 2002-09-06 18:41:55 +0000 | [diff] [blame] | 31 | #include "llvm/Pass.h" |
| 32 | #include "llvm/Function.h" |
Chris Lattner | d23520c | 2003-11-10 04:10:50 +0000 | [diff] [blame] | 33 | #include "llvm/Instructions.h" |
Chris Lattner | 5585b33 | 2004-01-12 19:12:50 +0000 | [diff] [blame] | 34 | #include "llvm/Type.h" |
Chris Lattner | b0dbd7f | 2002-09-06 18:41:55 +0000 | [diff] [blame] | 35 | #include "llvm/Analysis/Dominators.h" |
Chris Lattner | d23520c | 2003-11-10 04:10:50 +0000 | [diff] [blame] | 36 | #include "llvm/Assembly/Writer.h" |
Chris Lattner | b0dbd7f | 2002-09-06 18:41:55 +0000 | [diff] [blame] | 37 | #include "llvm/Transforms/Utils/Local.h" |
Chris Lattner | d23520c | 2003-11-10 04:10:50 +0000 | [diff] [blame] | 38 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
Chris Lattner | b0dbd7f | 2002-09-06 18:41:55 +0000 | [diff] [blame] | 39 | #include "llvm/Support/ConstantRange.h" |
| 40 | #include "llvm/Support/CFG.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 41 | #include "llvm/Support/Debug.h" |
| 42 | #include "llvm/ADT/PostOrderIterator.h" |
| 43 | #include "llvm/ADT/Statistic.h" |
Chris Lattner | b0dbd7f | 2002-09-06 18:41:55 +0000 | [diff] [blame] | 44 | #include <algorithm> |
Chris Lattner | dac58ad | 2006-01-22 23:32:06 +0000 | [diff] [blame^] | 45 | #include <iostream> |
Chris Lattner | d745602 | 2004-01-09 06:02:20 +0000 | [diff] [blame] | 46 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 47 | |
Chris Lattner | b0dbd7f | 2002-09-06 18:41:55 +0000 | [diff] [blame] | 48 | namespace { |
Chris Lattner | a92f696 | 2002-10-01 22:38:41 +0000 | [diff] [blame] | 49 | Statistic<> NumSetCCRemoved("cee", "Number of setcc instruction eliminated"); |
Chris Lattner | 065a616 | 2003-09-10 05:29:43 +0000 | [diff] [blame] | 50 | Statistic<> NumOperandsCann("cee", "Number of operands canonicalized"); |
Chris Lattner | a92f696 | 2002-10-01 22:38:41 +0000 | [diff] [blame] | 51 | Statistic<> BranchRevectors("cee", "Number of branches revectored"); |
Chris Lattner | b0dbd7f | 2002-09-06 18:41:55 +0000 | [diff] [blame] | 52 | |
| 53 | class ValueInfo; |
| 54 | class Relation { |
| 55 | Value *Val; // Relation to what value? |
| 56 | Instruction::BinaryOps Rel; // SetCC relation, or Add if no information |
| 57 | public: |
| 58 | Relation(Value *V) : Val(V), Rel(Instruction::Add) {} |
| 59 | bool operator<(const Relation &R) const { return Val < R.Val; } |
| 60 | Value *getValue() const { return Val; } |
| 61 | Instruction::BinaryOps getRelation() const { return Rel; } |
| 62 | |
| 63 | // contradicts - Return true if the relationship specified by the operand |
| 64 | // contradicts already known information. |
| 65 | // |
| 66 | bool contradicts(Instruction::BinaryOps Rel, const ValueInfo &VI) const; |
| 67 | |
| 68 | // incorporate - Incorporate information in the argument into this relation |
| 69 | // entry. This assumes that the information doesn't contradict itself. If |
| 70 | // any new information is gained, true is returned, otherwise false is |
| 71 | // returned to indicate that nothing was updated. |
| 72 | // |
| 73 | bool incorporate(Instruction::BinaryOps Rel, ValueInfo &VI); |
| 74 | |
| 75 | // KnownResult - Whether or not this condition determines the result of a |
| 76 | // setcc in the program. False & True are intentionally 0 & 1 so we can |
| 77 | // convert to bool by casting after checking for unknown. |
| 78 | // |
| 79 | enum KnownResult { KnownFalse = 0, KnownTrue = 1, Unknown = 2 }; |
| 80 | |
| 81 | // getImpliedResult - If this relationship between two values implies that |
| 82 | // the specified relationship is true or false, return that. If we cannot |
| 83 | // determine the result required, return Unknown. |
| 84 | // |
| 85 | KnownResult getImpliedResult(Instruction::BinaryOps Rel) const; |
| 86 | |
| 87 | // print - Output this relation to the specified stream |
| 88 | void print(std::ostream &OS) const; |
| 89 | void dump() const; |
| 90 | }; |
| 91 | |
| 92 | |
| 93 | // ValueInfo - One instance of this record exists for every value with |
| 94 | // relationships between other values. It keeps track of all of the |
| 95 | // relationships to other values in the program (specified with Relation) that |
| 96 | // are known to be valid in a region. |
| 97 | // |
| 98 | class ValueInfo { |
| 99 | // RelationShips - this value is know to have the specified relationships to |
| 100 | // other values. There can only be one entry per value, and this list is |
| 101 | // kept sorted by the Val field. |
| 102 | std::vector<Relation> Relationships; |
| 103 | |
Misha Brukman | a3bbcb5 | 2002-10-29 23:06:16 +0000 | [diff] [blame] | 104 | // If information about this value is known or propagated from constant |
Chris Lattner | b0dbd7f | 2002-09-06 18:41:55 +0000 | [diff] [blame] | 105 | // expressions, this range contains the possible values this value may hold. |
| 106 | ConstantRange Bounds; |
| 107 | |
| 108 | // If we find that this value is equal to another value that has a lower |
| 109 | // rank, this value is used as it's replacement. |
| 110 | // |
| 111 | Value *Replacement; |
| 112 | public: |
| 113 | ValueInfo(const Type *Ty) |
| 114 | : Bounds(Ty->isIntegral() ? Ty : Type::IntTy), Replacement(0) {} |
| 115 | |
| 116 | // getBounds() - Return the constant bounds of the value... |
| 117 | const ConstantRange &getBounds() const { return Bounds; } |
| 118 | ConstantRange &getBounds() { return Bounds; } |
| 119 | |
| 120 | const std::vector<Relation> &getRelationships() { return Relationships; } |
| 121 | |
| 122 | // getReplacement - Return the value this value is to be replaced with if it |
| 123 | // exists, otherwise return null. |
| 124 | // |
| 125 | Value *getReplacement() const { return Replacement; } |
| 126 | |
| 127 | // setReplacement - Used by the replacement calculation pass to figure out |
| 128 | // what to replace this value with, if anything. |
| 129 | // |
| 130 | void setReplacement(Value *Repl) { Replacement = Repl; } |
| 131 | |
| 132 | // getRelation - return the relationship entry for the specified value. |
Misha Brukman | cf00c4a | 2003-10-10 17:57:28 +0000 | [diff] [blame] | 133 | // This can invalidate references to other Relations, so use it carefully. |
Chris Lattner | b0dbd7f | 2002-09-06 18:41:55 +0000 | [diff] [blame] | 134 | // |
| 135 | Relation &getRelation(Value *V) { |
| 136 | // Binary search for V's entry... |
| 137 | std::vector<Relation>::iterator I = |
| 138 | std::lower_bound(Relationships.begin(), Relationships.end(), V); |
| 139 | |
| 140 | // If we found the entry, return it... |
| 141 | if (I != Relationships.end() && I->getValue() == V) |
| 142 | return *I; |
| 143 | |
| 144 | // Insert and return the new relationship... |
| 145 | return *Relationships.insert(I, V); |
| 146 | } |
| 147 | |
| 148 | const Relation *requestRelation(Value *V) const { |
| 149 | // Binary search for V's entry... |
| 150 | std::vector<Relation>::const_iterator I = |
| 151 | std::lower_bound(Relationships.begin(), Relationships.end(), V); |
| 152 | if (I != Relationships.end() && I->getValue() == V) |
| 153 | return &*I; |
| 154 | return 0; |
| 155 | } |
| 156 | |
| 157 | // print - Output information about this value relation... |
| 158 | void print(std::ostream &OS, Value *V) const; |
| 159 | void dump() const; |
| 160 | }; |
| 161 | |
| 162 | // RegionInfo - Keeps track of all of the value relationships for a region. A |
| 163 | // region is the are dominated by a basic block. RegionInfo's keep track of |
| 164 | // the RegionInfo for their dominator, because anything known in a dominator |
| 165 | // is known to be true in a dominated block as well. |
| 166 | // |
| 167 | class RegionInfo { |
| 168 | BasicBlock *BB; |
| 169 | |
| 170 | // ValueMap - Tracks the ValueInformation known for this region |
| 171 | typedef std::map<Value*, ValueInfo> ValueMapTy; |
| 172 | ValueMapTy ValueMap; |
| 173 | public: |
| 174 | RegionInfo(BasicBlock *bb) : BB(bb) {} |
| 175 | |
| 176 | // getEntryBlock - Return the block that dominates all of the members of |
| 177 | // this region. |
| 178 | BasicBlock *getEntryBlock() const { return BB; } |
| 179 | |
Chris Lattner | f7f009d | 2002-10-08 21:34:15 +0000 | [diff] [blame] | 180 | // empty - return true if this region has no information known about it. |
| 181 | bool empty() const { return ValueMap.empty(); } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 182 | |
Chris Lattner | b0dbd7f | 2002-09-06 18:41:55 +0000 | [diff] [blame] | 183 | const RegionInfo &operator=(const RegionInfo &RI) { |
| 184 | ValueMap = RI.ValueMap; |
| 185 | return *this; |
| 186 | } |
| 187 | |
| 188 | // print - Output information about this region... |
| 189 | void print(std::ostream &OS) const; |
Chris Lattner | f7f009d | 2002-10-08 21:34:15 +0000 | [diff] [blame] | 190 | void dump() const; |
Chris Lattner | b0dbd7f | 2002-09-06 18:41:55 +0000 | [diff] [blame] | 191 | |
| 192 | // Allow external access. |
| 193 | typedef ValueMapTy::iterator iterator; |
| 194 | iterator begin() { return ValueMap.begin(); } |
| 195 | iterator end() { return ValueMap.end(); } |
| 196 | |
| 197 | ValueInfo &getValueInfo(Value *V) { |
| 198 | ValueMapTy::iterator I = ValueMap.lower_bound(V); |
| 199 | if (I != ValueMap.end() && I->first == V) return I->second; |
| 200 | return ValueMap.insert(I, std::make_pair(V, V->getType()))->second; |
| 201 | } |
| 202 | |
| 203 | const ValueInfo *requestValueInfo(Value *V) const { |
| 204 | ValueMapTy::const_iterator I = ValueMap.find(V); |
| 205 | if (I != ValueMap.end()) return &I->second; |
| 206 | return 0; |
| 207 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 208 | |
Chris Lattner | f7f009d | 2002-10-08 21:34:15 +0000 | [diff] [blame] | 209 | /// removeValueInfo - Remove anything known about V from our records. This |
| 210 | /// works whether or not we know anything about V. |
| 211 | /// |
| 212 | void removeValueInfo(Value *V) { |
| 213 | ValueMap.erase(V); |
| 214 | } |
Chris Lattner | b0dbd7f | 2002-09-06 18:41:55 +0000 | [diff] [blame] | 215 | }; |
| 216 | |
| 217 | /// CEE - Correlated Expression Elimination |
| 218 | class CEE : public FunctionPass { |
| 219 | std::map<Value*, unsigned> RankMap; |
| 220 | std::map<BasicBlock*, RegionInfo> RegionInfoMap; |
Chris Lattner | 19ef3d5 | 2006-01-11 05:09:40 +0000 | [diff] [blame] | 221 | ETForest *EF; |
Chris Lattner | b0dbd7f | 2002-09-06 18:41:55 +0000 | [diff] [blame] | 222 | DominatorTree *DT; |
| 223 | public: |
| 224 | virtual bool runOnFunction(Function &F); |
| 225 | |
| 226 | // We don't modify the program, so we preserve all analyses |
| 227 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Chris Lattner | 19ef3d5 | 2006-01-11 05:09:40 +0000 | [diff] [blame] | 228 | AU.addRequired<ETForest>(); |
Chris Lattner | b0dbd7f | 2002-09-06 18:41:55 +0000 | [diff] [blame] | 229 | AU.addRequired<DominatorTree>(); |
Chris Lattner | 16e7a52 | 2002-09-24 15:43:56 +0000 | [diff] [blame] | 230 | AU.addRequiredID(BreakCriticalEdgesID); |
Chris Lattner | b0dbd7f | 2002-09-06 18:41:55 +0000 | [diff] [blame] | 231 | }; |
| 232 | |
| 233 | // print - Implement the standard print form to print out analysis |
| 234 | // information. |
| 235 | virtual void print(std::ostream &O, const Module *M) const; |
| 236 | |
Chris Lattner | b0dbd7f | 2002-09-06 18:41:55 +0000 | [diff] [blame] | 237 | private: |
| 238 | RegionInfo &getRegionInfo(BasicBlock *BB) { |
| 239 | std::map<BasicBlock*, RegionInfo>::iterator I |
| 240 | = RegionInfoMap.lower_bound(BB); |
| 241 | if (I != RegionInfoMap.end() && I->first == BB) return I->second; |
| 242 | return RegionInfoMap.insert(I, std::make_pair(BB, BB))->second; |
| 243 | } |
| 244 | |
| 245 | void BuildRankMap(Function &F); |
| 246 | unsigned getRank(Value *V) const { |
Reid Spencer | 48dc46a | 2004-07-18 00:29:57 +0000 | [diff] [blame] | 247 | if (isa<Constant>(V)) return 0; |
Chris Lattner | b0dbd7f | 2002-09-06 18:41:55 +0000 | [diff] [blame] | 248 | std::map<Value*, unsigned>::const_iterator I = RankMap.find(V); |
| 249 | if (I != RankMap.end()) return I->second; |
| 250 | return 0; // Must be some other global thing |
| 251 | } |
| 252 | |
| 253 | bool TransformRegion(BasicBlock *BB, std::set<BasicBlock*> &VisitedBlocks); |
| 254 | |
Chris Lattner | f7f009d | 2002-10-08 21:34:15 +0000 | [diff] [blame] | 255 | bool ForwardCorrelatedEdgeDestination(TerminatorInst *TI, unsigned SuccNo, |
| 256 | RegionInfo &RI); |
| 257 | |
| 258 | void ForwardSuccessorTo(TerminatorInst *TI, unsigned Succ, BasicBlock *D, |
| 259 | RegionInfo &RI); |
| 260 | void ReplaceUsesOfValueInRegion(Value *Orig, Value *New, |
| 261 | BasicBlock *RegionDominator); |
| 262 | void CalculateRegionExitBlocks(BasicBlock *BB, BasicBlock *OldSucc, |
| 263 | std::vector<BasicBlock*> &RegionExitBlocks); |
| 264 | void InsertRegionExitMerges(PHINode *NewPHI, Instruction *OldVal, |
| 265 | const std::vector<BasicBlock*> &RegionExitBlocks); |
| 266 | |
Misha Brukman | a3bbcb5 | 2002-10-29 23:06:16 +0000 | [diff] [blame] | 267 | void PropagateBranchInfo(BranchInst *BI); |
| 268 | void PropagateEquality(Value *Op0, Value *Op1, RegionInfo &RI); |
| 269 | void PropagateRelation(Instruction::BinaryOps Opcode, Value *Op0, |
Chris Lattner | b0dbd7f | 2002-09-06 18:41:55 +0000 | [diff] [blame] | 270 | Value *Op1, RegionInfo &RI); |
| 271 | void UpdateUsersOfValue(Value *V, RegionInfo &RI); |
| 272 | void IncorporateInstruction(Instruction *Inst, RegionInfo &RI); |
| 273 | void ComputeReplacements(RegionInfo &RI); |
| 274 | |
| 275 | |
| 276 | // getSetCCResult - Given a setcc instruction, determine if the result is |
| 277 | // determined by facts we already know about the region under analysis. |
| 278 | // Return KnownTrue, KnownFalse, or Unknown based on what we can determine. |
| 279 | // |
| 280 | Relation::KnownResult getSetCCResult(SetCondInst *SC, const RegionInfo &RI); |
| 281 | |
| 282 | |
| 283 | bool SimplifyBasicBlock(BasicBlock &BB, const RegionInfo &RI); |
| 284 | bool SimplifyInstruction(Instruction *Inst, const RegionInfo &RI); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 285 | }; |
Chris Lattner | b0dbd7f | 2002-09-06 18:41:55 +0000 | [diff] [blame] | 286 | RegisterOpt<CEE> X("cee", "Correlated Expression Elimination"); |
| 287 | } |
| 288 | |
Chris Lattner | 4b50156 | 2004-09-20 04:43:15 +0000 | [diff] [blame] | 289 | FunctionPass *llvm::createCorrelatedExpressionEliminationPass() { |
| 290 | return new CEE(); |
| 291 | } |
Chris Lattner | b0dbd7f | 2002-09-06 18:41:55 +0000 | [diff] [blame] | 292 | |
| 293 | |
| 294 | bool CEE::runOnFunction(Function &F) { |
| 295 | // Build a rank map for the function... |
| 296 | BuildRankMap(F); |
| 297 | |
| 298 | // Traverse the dominator tree, computing information for each node in the |
| 299 | // tree. Note that our traversal will not even touch unreachable basic |
| 300 | // blocks. |
Chris Lattner | 19ef3d5 | 2006-01-11 05:09:40 +0000 | [diff] [blame] | 301 | EF = &getAnalysis<ETForest>(); |
Chris Lattner | b0dbd7f | 2002-09-06 18:41:55 +0000 | [diff] [blame] | 302 | DT = &getAnalysis<DominatorTree>(); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 303 | |
Chris Lattner | b0dbd7f | 2002-09-06 18:41:55 +0000 | [diff] [blame] | 304 | std::set<BasicBlock*> VisitedBlocks; |
Chris Lattner | 02a3be0 | 2003-09-20 14:39:18 +0000 | [diff] [blame] | 305 | bool Changed = TransformRegion(&F.getEntryBlock(), VisitedBlocks); |
Chris Lattner | bd78696 | 2002-09-08 18:55:04 +0000 | [diff] [blame] | 306 | |
| 307 | RegionInfoMap.clear(); |
| 308 | RankMap.clear(); |
| 309 | return Changed; |
Chris Lattner | b0dbd7f | 2002-09-06 18:41:55 +0000 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | // TransformRegion - Transform the region starting with BB according to the |
| 313 | // calculated region information for the block. Transforming the region |
| 314 | // involves analyzing any information this block provides to successors, |
Misha Brukman | 82c89b9 | 2003-05-20 21:01:22 +0000 | [diff] [blame] | 315 | // propagating the information to successors, and finally transforming |
Chris Lattner | b0dbd7f | 2002-09-06 18:41:55 +0000 | [diff] [blame] | 316 | // successors. |
| 317 | // |
| 318 | // This method processes the function in depth first order, which guarantees |
| 319 | // that we process the immediate dominator of a block before the block itself. |
| 320 | // Because we are passing information from immediate dominators down to |
| 321 | // dominatees, we obviously have to process the information source before the |
| 322 | // information consumer. |
| 323 | // |
| 324 | bool CEE::TransformRegion(BasicBlock *BB, std::set<BasicBlock*> &VisitedBlocks){ |
| 325 | // Prevent infinite recursion... |
| 326 | if (VisitedBlocks.count(BB)) return false; |
| 327 | VisitedBlocks.insert(BB); |
| 328 | |
| 329 | // Get the computed region information for this block... |
| 330 | RegionInfo &RI = getRegionInfo(BB); |
| 331 | |
| 332 | // Compute the replacement information for this block... |
| 333 | ComputeReplacements(RI); |
| 334 | |
| 335 | // If debugging, print computed region information... |
| 336 | DEBUG(RI.print(std::cerr)); |
| 337 | |
| 338 | // Simplify the contents of this block... |
| 339 | bool Changed = SimplifyBasicBlock(*BB, RI); |
| 340 | |
| 341 | // Get the terminator of this basic block... |
| 342 | TerminatorInst *TI = BB->getTerminator(); |
| 343 | |
Chris Lattner | b0dbd7f | 2002-09-06 18:41:55 +0000 | [diff] [blame] | 344 | // Loop over all of the blocks that this block is the immediate dominator for. |
| 345 | // Because all information known in this region is also known in all of the |
Misha Brukman | a3bbcb5 | 2002-10-29 23:06:16 +0000 | [diff] [blame] | 346 | // blocks that are dominated by this one, we can safely propagate the |
Chris Lattner | b0dbd7f | 2002-09-06 18:41:55 +0000 | [diff] [blame] | 347 | // information down now. |
| 348 | // |
| 349 | DominatorTree::Node *BBN = (*DT)[BB]; |
Misha Brukman | a3bbcb5 | 2002-10-29 23:06:16 +0000 | [diff] [blame] | 350 | if (!RI.empty()) // Time opt: only propagate if we can change something |
Chris Lattner | f7f009d | 2002-10-08 21:34:15 +0000 | [diff] [blame] | 351 | for (unsigned i = 0, e = BBN->getChildren().size(); i != e; ++i) { |
Chris Lattner | c444a42 | 2003-09-11 16:26:13 +0000 | [diff] [blame] | 352 | BasicBlock *Dominated = BBN->getChildren()[i]->getBlock(); |
Chris Lattner | f7f009d | 2002-10-08 21:34:15 +0000 | [diff] [blame] | 353 | assert(RegionInfoMap.find(Dominated) == RegionInfoMap.end() && |
| 354 | "RegionInfo should be calculated in dominanace order!"); |
| 355 | getRegionInfo(Dominated) = RI; |
| 356 | } |
Chris Lattner | b0dbd7f | 2002-09-06 18:41:55 +0000 | [diff] [blame] | 357 | |
| 358 | // Now that all of our successors have information if they deserve it, |
Misha Brukman | a3bbcb5 | 2002-10-29 23:06:16 +0000 | [diff] [blame] | 359 | // propagate any information our terminator instruction finds to our |
Chris Lattner | b0dbd7f | 2002-09-06 18:41:55 +0000 | [diff] [blame] | 360 | // successors. |
| 361 | if (BranchInst *BI = dyn_cast<BranchInst>(TI)) |
| 362 | if (BI->isConditional()) |
Misha Brukman | a3bbcb5 | 2002-10-29 23:06:16 +0000 | [diff] [blame] | 363 | PropagateBranchInfo(BI); |
Chris Lattner | b0dbd7f | 2002-09-06 18:41:55 +0000 | [diff] [blame] | 364 | |
| 365 | // If this is a branch to a block outside our region that simply performs |
| 366 | // another conditional branch, one whose outcome is known inside of this |
| 367 | // region, then vector this outgoing edge directly to the known destination. |
| 368 | // |
Chris Lattner | c017d91 | 2002-09-23 20:06:22 +0000 | [diff] [blame] | 369 | for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) |
Chris Lattner | f7f009d | 2002-10-08 21:34:15 +0000 | [diff] [blame] | 370 | while (ForwardCorrelatedEdgeDestination(TI, i, RI)) { |
Chris Lattner | b0dbd7f | 2002-09-06 18:41:55 +0000 | [diff] [blame] | 371 | ++BranchRevectors; |
Chris Lattner | c017d91 | 2002-09-23 20:06:22 +0000 | [diff] [blame] | 372 | Changed = true; |
Chris Lattner | b0dbd7f | 2002-09-06 18:41:55 +0000 | [diff] [blame] | 373 | } |
Chris Lattner | b0dbd7f | 2002-09-06 18:41:55 +0000 | [diff] [blame] | 374 | |
| 375 | // Now that all of our successors have information, recursively process them. |
| 376 | for (unsigned i = 0, e = BBN->getChildren().size(); i != e; ++i) |
Chris Lattner | c444a42 | 2003-09-11 16:26:13 +0000 | [diff] [blame] | 377 | Changed |= TransformRegion(BBN->getChildren()[i]->getBlock(),VisitedBlocks); |
Chris Lattner | b0dbd7f | 2002-09-06 18:41:55 +0000 | [diff] [blame] | 378 | |
Chris Lattner | b0dbd7f | 2002-09-06 18:41:55 +0000 | [diff] [blame] | 379 | return Changed; |
| 380 | } |
| 381 | |
Chris Lattner | f7f009d | 2002-10-08 21:34:15 +0000 | [diff] [blame] | 382 | // isBlockSimpleEnoughForCheck to see if the block is simple enough for us to |
| 383 | // revector the conditional branch in the bottom of the block, do so now. |
Chris Lattner | b0dbd7f | 2002-09-06 18:41:55 +0000 | [diff] [blame] | 384 | // |
Chris Lattner | f7f009d | 2002-10-08 21:34:15 +0000 | [diff] [blame] | 385 | static bool isBlockSimpleEnough(BasicBlock *BB) { |
| 386 | assert(isa<BranchInst>(BB->getTerminator())); |
| 387 | BranchInst *BI = cast<BranchInst>(BB->getTerminator()); |
| 388 | assert(BI->isConditional()); |
| 389 | |
| 390 | // Check the common case first: empty block, or block with just a setcc. |
| 391 | if (BB->size() == 1 || |
| 392 | (BB->size() == 2 && &BB->front() == BI->getCondition() && |
Chris Lattner | fd05924 | 2003-10-15 16:48:29 +0000 | [diff] [blame] | 393 | BI->getCondition()->hasOneUse())) |
Chris Lattner | f7f009d | 2002-10-08 21:34:15 +0000 | [diff] [blame] | 394 | return true; |
| 395 | |
| 396 | // Check the more complex case now... |
| 397 | BasicBlock::iterator I = BB->begin(); |
| 398 | |
| 399 | // FIXME: This should be reenabled once the regression with SIM is fixed! |
| 400 | #if 0 |
| 401 | // PHI Nodes are ok, just skip over them... |
| 402 | while (isa<PHINode>(*I)) ++I; |
| 403 | #endif |
| 404 | |
| 405 | // Accept the setcc instruction... |
| 406 | if (&*I == BI->getCondition()) |
| 407 | ++I; |
| 408 | |
| 409 | // Nothing else is acceptable here yet. We must not revector... unless we are |
| 410 | // at the terminator instruction. |
| 411 | if (&*I == BI) |
| 412 | return true; |
| 413 | |
| 414 | return false; |
| 415 | } |
| 416 | |
| 417 | |
| 418 | bool CEE::ForwardCorrelatedEdgeDestination(TerminatorInst *TI, unsigned SuccNo, |
| 419 | RegionInfo &RI) { |
| 420 | // If this successor is a simple block not in the current region, which |
| 421 | // contains only a conditional branch, we decide if the outcome of the branch |
| 422 | // can be determined from information inside of the region. Instead of going |
| 423 | // to this block, we can instead go to the destination we know is the right |
| 424 | // target. |
| 425 | // |
| 426 | |
Chris Lattner | b0dbd7f | 2002-09-06 18:41:55 +0000 | [diff] [blame] | 427 | // Check to see if we dominate the block. If so, this block will get the |
| 428 | // condition turned to a constant anyway. |
| 429 | // |
Chris Lattner | 19ef3d5 | 2006-01-11 05:09:40 +0000 | [diff] [blame] | 430 | //if (EF->dominates(RI.getEntryBlock(), BB)) |
Chris Lattner | b0dbd7f | 2002-09-06 18:41:55 +0000 | [diff] [blame] | 431 | // return 0; |
| 432 | |
Chris Lattner | f7f009d | 2002-10-08 21:34:15 +0000 | [diff] [blame] | 433 | BasicBlock *BB = TI->getParent(); |
| 434 | |
| 435 | // Get the destination block of this edge... |
| 436 | BasicBlock *OldSucc = TI->getSuccessor(SuccNo); |
| 437 | |
| 438 | // Make sure that the block ends with a conditional branch and is simple |
| 439 | // enough for use to be able to revector over. |
| 440 | BranchInst *BI = dyn_cast<BranchInst>(OldSucc->getTerminator()); |
| 441 | if (BI == 0 || !BI->isConditional() || !isBlockSimpleEnough(OldSucc)) |
| 442 | return false; |
| 443 | |
| 444 | // We can only forward the branch over the block if the block ends with a |
| 445 | // setcc we can determine the outcome for. |
| 446 | // |
| 447 | // FIXME: we can make this more generic. Code below already handles more |
| 448 | // generic case. |
| 449 | SetCondInst *SCI = dyn_cast<SetCondInst>(BI->getCondition()); |
| 450 | if (SCI == 0) return false; |
| 451 | |
| 452 | // Make a new RegionInfo structure so that we can simulate the effect of the |
| 453 | // PHI nodes in the block we are skipping over... |
| 454 | // |
| 455 | RegionInfo NewRI(RI); |
| 456 | |
| 457 | // Remove value information for all of the values we are simulating... to make |
| 458 | // sure we don't have any stale information. |
| 459 | for (BasicBlock::iterator I = OldSucc->begin(), E = OldSucc->end(); I!=E; ++I) |
| 460 | if (I->getType() != Type::VoidTy) |
| 461 | NewRI.removeValueInfo(I); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 462 | |
Chris Lattner | f7f009d | 2002-10-08 21:34:15 +0000 | [diff] [blame] | 463 | // Put the newly discovered information into the RegionInfo... |
| 464 | for (BasicBlock::iterator I = OldSucc->begin(), E = OldSucc->end(); I!=E; ++I) |
Chris Lattner | e408e25 | 2003-04-23 16:37:45 +0000 | [diff] [blame] | 465 | if (PHINode *PN = dyn_cast<PHINode>(I)) { |
Chris Lattner | f7f009d | 2002-10-08 21:34:15 +0000 | [diff] [blame] | 466 | int OpNum = PN->getBasicBlockIndex(BB); |
| 467 | assert(OpNum != -1 && "PHI doesn't have incoming edge for predecessor!?"); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 468 | PropagateEquality(PN, PN->getIncomingValue(OpNum), NewRI); |
Chris Lattner | e408e25 | 2003-04-23 16:37:45 +0000 | [diff] [blame] | 469 | } else if (SetCondInst *SCI = dyn_cast<SetCondInst>(I)) { |
Chris Lattner | f7f009d | 2002-10-08 21:34:15 +0000 | [diff] [blame] | 470 | Relation::KnownResult Res = getSetCCResult(SCI, NewRI); |
| 471 | if (Res == Relation::Unknown) return false; |
Misha Brukman | a3bbcb5 | 2002-10-29 23:06:16 +0000 | [diff] [blame] | 472 | PropagateEquality(SCI, ConstantBool::get(Res), NewRI); |
Chris Lattner | f7f009d | 2002-10-08 21:34:15 +0000 | [diff] [blame] | 473 | } else { |
| 474 | assert(isa<BranchInst>(*I) && "Unexpected instruction type!"); |
Chris Lattner | b0dbd7f | 2002-09-06 18:41:55 +0000 | [diff] [blame] | 475 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 476 | |
Chris Lattner | f7f009d | 2002-10-08 21:34:15 +0000 | [diff] [blame] | 477 | // Compute the facts implied by what we have discovered... |
| 478 | ComputeReplacements(NewRI); |
| 479 | |
| 480 | ValueInfo &PredicateVI = NewRI.getValueInfo(BI->getCondition()); |
| 481 | if (PredicateVI.getReplacement() && |
Reid Spencer | 48dc46a | 2004-07-18 00:29:57 +0000 | [diff] [blame] | 482 | isa<Constant>(PredicateVI.getReplacement()) && |
| 483 | !isa<GlobalValue>(PredicateVI.getReplacement())) { |
Chris Lattner | f7f009d | 2002-10-08 21:34:15 +0000 | [diff] [blame] | 484 | ConstantBool *CB = cast<ConstantBool>(PredicateVI.getReplacement()); |
| 485 | |
| 486 | // Forward to the successor that corresponds to the branch we will take. |
| 487 | ForwardSuccessorTo(TI, SuccNo, BI->getSuccessor(!CB->getValue()), NewRI); |
| 488 | return true; |
| 489 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 490 | |
Chris Lattner | f7f009d | 2002-10-08 21:34:15 +0000 | [diff] [blame] | 491 | return false; |
Chris Lattner | b0dbd7f | 2002-09-06 18:41:55 +0000 | [diff] [blame] | 492 | } |
| 493 | |
Chris Lattner | f7f009d | 2002-10-08 21:34:15 +0000 | [diff] [blame] | 494 | static Value *getReplacementOrValue(Value *V, RegionInfo &RI) { |
| 495 | if (const ValueInfo *VI = RI.requestValueInfo(V)) |
| 496 | if (Value *Repl = VI->getReplacement()) |
| 497 | return Repl; |
| 498 | return V; |
| 499 | } |
| 500 | |
| 501 | /// ForwardSuccessorTo - We have found that we can forward successor # 'SuccNo' |
| 502 | /// of Terminator 'TI' to the 'Dest' BasicBlock. This method performs the |
| 503 | /// mechanics of updating SSA information and revectoring the branch. |
| 504 | /// |
| 505 | void CEE::ForwardSuccessorTo(TerminatorInst *TI, unsigned SuccNo, |
| 506 | BasicBlock *Dest, RegionInfo &RI) { |
| 507 | // If there are any PHI nodes in the Dest BB, we must duplicate the entry |
| 508 | // in the PHI node for the old successor to now include an entry from the |
| 509 | // current basic block. |
| 510 | // |
| 511 | BasicBlock *OldSucc = TI->getSuccessor(SuccNo); |
| 512 | BasicBlock *BB = TI->getParent(); |
| 513 | |
| 514 | DEBUG(std::cerr << "Forwarding branch in basic block %" << BB->getName() |
| 515 | << " from block %" << OldSucc->getName() << " to block %" |
| 516 | << Dest->getName() << "\n"); |
| 517 | |
| 518 | DEBUG(std::cerr << "Before forwarding: " << *BB->getParent()); |
| 519 | |
| 520 | // Because we know that there cannot be critical edges in the flow graph, and |
| 521 | // that OldSucc has multiple outgoing edges, this means that Dest cannot have |
| 522 | // multiple incoming edges. |
| 523 | // |
| 524 | #ifndef NDEBUG |
| 525 | pred_iterator DPI = pred_begin(Dest); ++DPI; |
| 526 | assert(DPI == pred_end(Dest) && "Critical edge found!!"); |
| 527 | #endif |
| 528 | |
| 529 | // Loop over any PHI nodes in the destination, eliminating them, because they |
| 530 | // may only have one input. |
| 531 | // |
| 532 | while (PHINode *PN = dyn_cast<PHINode>(&Dest->front())) { |
| 533 | assert(PN->getNumIncomingValues() == 1 && "Crit edge found!"); |
| 534 | // Eliminate the PHI node |
| 535 | PN->replaceAllUsesWith(PN->getIncomingValue(0)); |
| 536 | Dest->getInstList().erase(PN); |
| 537 | } |
| 538 | |
| 539 | // If there are values defined in the "OldSucc" basic block, we need to insert |
| 540 | // PHI nodes in the regions we are dealing with to emulate them. This can |
| 541 | // insert dead phi nodes, but it is more trouble to see if they are used than |
| 542 | // to just blindly insert them. |
| 543 | // |
Chris Lattner | 19ef3d5 | 2006-01-11 05:09:40 +0000 | [diff] [blame] | 544 | if (EF->dominates(OldSucc, Dest)) { |
Chris Lattner | f7f009d | 2002-10-08 21:34:15 +0000 | [diff] [blame] | 545 | // RegionExitBlocks - Find all of the blocks that are not dominated by Dest, |
| 546 | // but have predecessors that are. Additionally, prune down the set to only |
| 547 | // include blocks that are dominated by OldSucc as well. |
| 548 | // |
| 549 | std::vector<BasicBlock*> RegionExitBlocks; |
| 550 | CalculateRegionExitBlocks(Dest, OldSucc, RegionExitBlocks); |
| 551 | |
| 552 | for (BasicBlock::iterator I = OldSucc->begin(), E = OldSucc->end(); |
| 553 | I != E; ++I) |
| 554 | if (I->getType() != Type::VoidTy) { |
| 555 | // Create and insert the PHI node into the top of Dest. |
| 556 | PHINode *NewPN = new PHINode(I->getType(), I->getName()+".fw_merge", |
| 557 | Dest->begin()); |
Chris Lattner | 51c20e9 | 2002-11-08 23:18:37 +0000 | [diff] [blame] | 558 | // There is definitely an edge from OldSucc... add the edge now |
Chris Lattner | f7f009d | 2002-10-08 21:34:15 +0000 | [diff] [blame] | 559 | NewPN->addIncoming(I, OldSucc); |
| 560 | |
| 561 | // There is also an edge from BB now, add the edge with the calculated |
| 562 | // value from the RI. |
| 563 | NewPN->addIncoming(getReplacementOrValue(I, RI), BB); |
| 564 | |
| 565 | // Make everything in the Dest region use the new PHI node now... |
| 566 | ReplaceUsesOfValueInRegion(I, NewPN, Dest); |
| 567 | |
| 568 | // Make sure that exits out of the region dominated by NewPN get PHI |
| 569 | // nodes that merge the values as appropriate. |
| 570 | InsertRegionExitMerges(NewPN, I, RegionExitBlocks); |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | // If there were PHI nodes in OldSucc, we need to remove the entry for this |
| 575 | // edge from the PHI node, and we need to replace any references to the PHI |
| 576 | // node with a new value. |
| 577 | // |
Reid Spencer | 2da5c3d | 2004-09-15 17:06:42 +0000 | [diff] [blame] | 578 | for (BasicBlock::iterator I = OldSucc->begin(); isa<PHINode>(I); ) { |
| 579 | PHINode *PN = cast<PHINode>(I); |
Chris Lattner | f7f009d | 2002-10-08 21:34:15 +0000 | [diff] [blame] | 580 | |
| 581 | // Get the value flowing across the old edge and remove the PHI node entry |
| 582 | // for this edge: we are about to remove the edge! Don't remove the PHI |
| 583 | // node yet though if this is the last edge into it. |
| 584 | Value *EdgeValue = PN->removeIncomingValue(BB, false); |
| 585 | |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 586 | // Make sure that anything that used to use PN now refers to EdgeValue |
Chris Lattner | f7f009d | 2002-10-08 21:34:15 +0000 | [diff] [blame] | 587 | ReplaceUsesOfValueInRegion(PN, EdgeValue, Dest); |
| 588 | |
| 589 | // If there is only one value left coming into the PHI node, replace the PHI |
| 590 | // node itself with the one incoming value left. |
| 591 | // |
| 592 | if (PN->getNumIncomingValues() == 1) { |
| 593 | assert(PN->getNumIncomingValues() == 1); |
| 594 | PN->replaceAllUsesWith(PN->getIncomingValue(0)); |
| 595 | PN->getParent()->getInstList().erase(PN); |
| 596 | I = OldSucc->begin(); |
| 597 | } else if (PN->getNumIncomingValues() == 0) { // Nuke the PHI |
| 598 | // If we removed the last incoming value to this PHI, nuke the PHI node |
| 599 | // now. |
| 600 | PN->replaceAllUsesWith(Constant::getNullValue(PN->getType())); |
| 601 | PN->getParent()->getInstList().erase(PN); |
| 602 | I = OldSucc->begin(); |
| 603 | } else { |
| 604 | ++I; // Otherwise, move on to the next PHI node |
| 605 | } |
| 606 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 607 | |
Chris Lattner | f7f009d | 2002-10-08 21:34:15 +0000 | [diff] [blame] | 608 | // Actually revector the branch now... |
| 609 | TI->setSuccessor(SuccNo, Dest); |
| 610 | |
| 611 | // If we just introduced a critical edge in the flow graph, make sure to break |
| 612 | // it right away... |
Chris Lattner | d23520c | 2003-11-10 04:10:50 +0000 | [diff] [blame] | 613 | SplitCriticalEdge(TI, SuccNo, this); |
Chris Lattner | f7f009d | 2002-10-08 21:34:15 +0000 | [diff] [blame] | 614 | |
| 615 | // Make sure that we don't introduce critical edges from oldsucc now! |
| 616 | for (unsigned i = 0, e = OldSucc->getTerminator()->getNumSuccessors(); |
| 617 | i != e; ++i) |
| 618 | if (isCriticalEdge(OldSucc->getTerminator(), i)) |
| 619 | SplitCriticalEdge(OldSucc->getTerminator(), i, this); |
| 620 | |
| 621 | // Since we invalidated the CFG, recalculate the dominator set so that it is |
| 622 | // useful for later processing! |
| 623 | // FIXME: This is much worse than it really should be! |
Chris Lattner | 19ef3d5 | 2006-01-11 05:09:40 +0000 | [diff] [blame] | 624 | //EF->recalculate(); |
Chris Lattner | f7f009d | 2002-10-08 21:34:15 +0000 | [diff] [blame] | 625 | |
| 626 | DEBUG(std::cerr << "After forwarding: " << *BB->getParent()); |
| 627 | } |
| 628 | |
| 629 | /// ReplaceUsesOfValueInRegion - This method replaces all uses of Orig with uses |
| 630 | /// of New. It only affects instructions that are defined in basic blocks that |
| 631 | /// are dominated by Head. |
| 632 | /// |
| 633 | void CEE::ReplaceUsesOfValueInRegion(Value *Orig, Value *New, |
| 634 | BasicBlock *RegionDominator) { |
| 635 | assert(Orig != New && "Cannot replace value with itself"); |
| 636 | std::vector<Instruction*> InstsToChange; |
| 637 | std::vector<PHINode*> PHIsToChange; |
Chris Lattner | ac93004 | 2005-02-01 01:23:49 +0000 | [diff] [blame] | 638 | InstsToChange.reserve(Orig->getNumUses()); |
Chris Lattner | f7f009d | 2002-10-08 21:34:15 +0000 | [diff] [blame] | 639 | |
| 640 | // Loop over instructions adding them to InstsToChange vector, this allows us |
| 641 | // an easy way to avoid invalidating the use_iterator at a bad time. |
| 642 | for (Value::use_iterator I = Orig->use_begin(), E = Orig->use_end(); |
| 643 | I != E; ++I) |
| 644 | if (Instruction *User = dyn_cast<Instruction>(*I)) |
Chris Lattner | 19ef3d5 | 2006-01-11 05:09:40 +0000 | [diff] [blame] | 645 | if (EF->dominates(RegionDominator, User->getParent())) |
Chris Lattner | f7f009d | 2002-10-08 21:34:15 +0000 | [diff] [blame] | 646 | InstsToChange.push_back(User); |
| 647 | else if (PHINode *PN = dyn_cast<PHINode>(User)) { |
| 648 | PHIsToChange.push_back(PN); |
| 649 | } |
| 650 | |
| 651 | // PHIsToChange contains PHI nodes that use Orig that do not live in blocks |
| 652 | // dominated by orig. If the block the value flows in from is dominated by |
| 653 | // RegionDominator, then we rewrite the PHI |
| 654 | for (unsigned i = 0, e = PHIsToChange.size(); i != e; ++i) { |
| 655 | PHINode *PN = PHIsToChange[i]; |
| 656 | for (unsigned j = 0, e = PN->getNumIncomingValues(); j != e; ++j) |
| 657 | if (PN->getIncomingValue(j) == Orig && |
Chris Lattner | 19ef3d5 | 2006-01-11 05:09:40 +0000 | [diff] [blame] | 658 | EF->dominates(RegionDominator, PN->getIncomingBlock(j))) |
Chris Lattner | f7f009d | 2002-10-08 21:34:15 +0000 | [diff] [blame] | 659 | PN->setIncomingValue(j, New); |
| 660 | } |
| 661 | |
| 662 | // Loop over the InstsToChange list, replacing all uses of Orig with uses of |
| 663 | // New. This list contains all of the instructions in our region that use |
| 664 | // Orig. |
| 665 | for (unsigned i = 0, e = InstsToChange.size(); i != e; ++i) |
| 666 | if (PHINode *PN = dyn_cast<PHINode>(InstsToChange[i])) { |
| 667 | // PHINodes must be handled carefully. If the PHI node itself is in the |
| 668 | // region, we have to make sure to only do the replacement for incoming |
| 669 | // values that correspond to basic blocks in the region. |
| 670 | for (unsigned j = 0, e = PN->getNumIncomingValues(); j != e; ++j) |
| 671 | if (PN->getIncomingValue(j) == Orig && |
Chris Lattner | 19ef3d5 | 2006-01-11 05:09:40 +0000 | [diff] [blame] | 672 | EF->dominates(RegionDominator, PN->getIncomingBlock(j))) |
Chris Lattner | f7f009d | 2002-10-08 21:34:15 +0000 | [diff] [blame] | 673 | PN->setIncomingValue(j, New); |
| 674 | |
| 675 | } else { |
| 676 | InstsToChange[i]->replaceUsesOfWith(Orig, New); |
| 677 | } |
| 678 | } |
| 679 | |
| 680 | static void CalcRegionExitBlocks(BasicBlock *Header, BasicBlock *BB, |
| 681 | std::set<BasicBlock*> &Visited, |
Chris Lattner | 19ef3d5 | 2006-01-11 05:09:40 +0000 | [diff] [blame] | 682 | ETForest &EF, |
Chris Lattner | f7f009d | 2002-10-08 21:34:15 +0000 | [diff] [blame] | 683 | std::vector<BasicBlock*> &RegionExitBlocks) { |
| 684 | if (Visited.count(BB)) return; |
| 685 | Visited.insert(BB); |
| 686 | |
Chris Lattner | 19ef3d5 | 2006-01-11 05:09:40 +0000 | [diff] [blame] | 687 | if (EF.dominates(Header, BB)) { // Block in the region, recursively traverse |
Chris Lattner | f7f009d | 2002-10-08 21:34:15 +0000 | [diff] [blame] | 688 | for (succ_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) |
Chris Lattner | 19ef3d5 | 2006-01-11 05:09:40 +0000 | [diff] [blame] | 689 | CalcRegionExitBlocks(Header, *I, Visited, EF, RegionExitBlocks); |
Chris Lattner | f7f009d | 2002-10-08 21:34:15 +0000 | [diff] [blame] | 690 | } else { |
| 691 | // Header does not dominate this block, but we have a predecessor that does |
| 692 | // dominate us. Add ourself to the list. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 693 | RegionExitBlocks.push_back(BB); |
Chris Lattner | f7f009d | 2002-10-08 21:34:15 +0000 | [diff] [blame] | 694 | } |
| 695 | } |
| 696 | |
| 697 | /// CalculateRegionExitBlocks - Find all of the blocks that are not dominated by |
| 698 | /// BB, but have predecessors that are. Additionally, prune down the set to |
| 699 | /// only include blocks that are dominated by OldSucc as well. |
| 700 | /// |
| 701 | void CEE::CalculateRegionExitBlocks(BasicBlock *BB, BasicBlock *OldSucc, |
| 702 | std::vector<BasicBlock*> &RegionExitBlocks){ |
| 703 | std::set<BasicBlock*> Visited; // Don't infinite loop |
| 704 | |
| 705 | // Recursively calculate blocks we are interested in... |
Chris Lattner | 19ef3d5 | 2006-01-11 05:09:40 +0000 | [diff] [blame] | 706 | CalcRegionExitBlocks(BB, BB, Visited, *EF, RegionExitBlocks); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 707 | |
Chris Lattner | f7f009d | 2002-10-08 21:34:15 +0000 | [diff] [blame] | 708 | // Filter out blocks that are not dominated by OldSucc... |
| 709 | for (unsigned i = 0; i != RegionExitBlocks.size(); ) { |
Chris Lattner | 19ef3d5 | 2006-01-11 05:09:40 +0000 | [diff] [blame] | 710 | if (EF->dominates(OldSucc, RegionExitBlocks[i])) |
Chris Lattner | f7f009d | 2002-10-08 21:34:15 +0000 | [diff] [blame] | 711 | ++i; // Block is ok, keep it. |
| 712 | else { |
| 713 | // Move to end of list... |
| 714 | std::swap(RegionExitBlocks[i], RegionExitBlocks.back()); |
| 715 | RegionExitBlocks.pop_back(); // Nuke the end |
| 716 | } |
| 717 | } |
| 718 | } |
| 719 | |
| 720 | void CEE::InsertRegionExitMerges(PHINode *BBVal, Instruction *OldVal, |
| 721 | const std::vector<BasicBlock*> &RegionExitBlocks) { |
| 722 | assert(BBVal->getType() == OldVal->getType() && "Should be derived values!"); |
| 723 | BasicBlock *BB = BBVal->getParent(); |
| 724 | BasicBlock *OldSucc = OldVal->getParent(); |
| 725 | |
| 726 | // Loop over all of the blocks we have to place PHIs in, doing it. |
| 727 | for (unsigned i = 0, e = RegionExitBlocks.size(); i != e; ++i) { |
| 728 | BasicBlock *FBlock = RegionExitBlocks[i]; // Block on the frontier |
| 729 | |
| 730 | // Create the new PHI node |
| 731 | PHINode *NewPN = new PHINode(BBVal->getType(), |
| 732 | OldVal->getName()+".fw_frontier", |
| 733 | FBlock->begin()); |
| 734 | |
| 735 | // Add an incoming value for every predecessor of the block... |
| 736 | for (pred_iterator PI = pred_begin(FBlock), PE = pred_end(FBlock); |
| 737 | PI != PE; ++PI) { |
| 738 | // If the incoming edge is from the region dominated by BB, use BBVal, |
| 739 | // otherwise use OldVal. |
Chris Lattner | 19ef3d5 | 2006-01-11 05:09:40 +0000 | [diff] [blame] | 740 | NewPN->addIncoming(EF->dominates(BB, *PI) ? BBVal : OldVal, *PI); |
Chris Lattner | f7f009d | 2002-10-08 21:34:15 +0000 | [diff] [blame] | 741 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 742 | |
Chris Lattner | f7f009d | 2002-10-08 21:34:15 +0000 | [diff] [blame] | 743 | // Now make everyone dominated by this block use this new value! |
| 744 | ReplaceUsesOfValueInRegion(OldVal, NewPN, FBlock); |
| 745 | } |
| 746 | } |
| 747 | |
| 748 | |
| 749 | |
Chris Lattner | b0dbd7f | 2002-09-06 18:41:55 +0000 | [diff] [blame] | 750 | // BuildRankMap - This method builds the rank map data structure which gives |
| 751 | // each instruction/value in the function a value based on how early it appears |
| 752 | // in the function. We give constants and globals rank 0, arguments are |
| 753 | // numbered starting at one, and instructions are numbered in reverse post-order |
| 754 | // from where the arguments leave off. This gives instructions in loops higher |
| 755 | // values than instructions not in loops. |
| 756 | // |
| 757 | void CEE::BuildRankMap(Function &F) { |
| 758 | unsigned Rank = 1; // Skip rank zero. |
| 759 | |
| 760 | // Number the arguments... |
Chris Lattner | e4d5c44 | 2005-03-15 04:54:21 +0000 | [diff] [blame] | 761 | for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) |
Chris Lattner | b0dbd7f | 2002-09-06 18:41:55 +0000 | [diff] [blame] | 762 | RankMap[I] = Rank++; |
| 763 | |
| 764 | // Number the instructions in reverse post order... |
| 765 | ReversePostOrderTraversal<Function*> RPOT(&F); |
| 766 | for (ReversePostOrderTraversal<Function*>::rpo_iterator I = RPOT.begin(), |
| 767 | E = RPOT.end(); I != E; ++I) |
| 768 | for (BasicBlock::iterator BBI = (*I)->begin(), E = (*I)->end(); |
| 769 | BBI != E; ++BBI) |
| 770 | if (BBI->getType() != Type::VoidTy) |
| 771 | RankMap[BBI] = Rank++; |
| 772 | } |
| 773 | |
| 774 | |
Misha Brukman | a3bbcb5 | 2002-10-29 23:06:16 +0000 | [diff] [blame] | 775 | // PropagateBranchInfo - When this method is invoked, we need to propagate |
Chris Lattner | b0dbd7f | 2002-09-06 18:41:55 +0000 | [diff] [blame] | 776 | // information derived from the branch condition into the true and false |
| 777 | // branches of BI. Since we know that there aren't any critical edges in the |
| 778 | // flow graph, this can proceed unconditionally. |
| 779 | // |
Misha Brukman | a3bbcb5 | 2002-10-29 23:06:16 +0000 | [diff] [blame] | 780 | void CEE::PropagateBranchInfo(BranchInst *BI) { |
Chris Lattner | b0dbd7f | 2002-09-06 18:41:55 +0000 | [diff] [blame] | 781 | assert(BI->isConditional() && "Must be a conditional branch!"); |
Chris Lattner | b0dbd7f | 2002-09-06 18:41:55 +0000 | [diff] [blame] | 782 | |
Misha Brukman | a3bbcb5 | 2002-10-29 23:06:16 +0000 | [diff] [blame] | 783 | // Propagate information into the true block... |
Chris Lattner | b0dbd7f | 2002-09-06 18:41:55 +0000 | [diff] [blame] | 784 | // |
Misha Brukman | a3bbcb5 | 2002-10-29 23:06:16 +0000 | [diff] [blame] | 785 | PropagateEquality(BI->getCondition(), ConstantBool::True, |
Chris Lattner | f7f009d | 2002-10-08 21:34:15 +0000 | [diff] [blame] | 786 | getRegionInfo(BI->getSuccessor(0))); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 787 | |
Misha Brukman | a3bbcb5 | 2002-10-29 23:06:16 +0000 | [diff] [blame] | 788 | // Propagate information into the false block... |
Chris Lattner | b0dbd7f | 2002-09-06 18:41:55 +0000 | [diff] [blame] | 789 | // |
Misha Brukman | a3bbcb5 | 2002-10-29 23:06:16 +0000 | [diff] [blame] | 790 | PropagateEquality(BI->getCondition(), ConstantBool::False, |
Chris Lattner | f7f009d | 2002-10-08 21:34:15 +0000 | [diff] [blame] | 791 | getRegionInfo(BI->getSuccessor(1))); |
Chris Lattner | b0dbd7f | 2002-09-06 18:41:55 +0000 | [diff] [blame] | 792 | } |
| 793 | |
| 794 | |
Misha Brukman | a3bbcb5 | 2002-10-29 23:06:16 +0000 | [diff] [blame] | 795 | // PropagateEquality - If we discover that two values are equal to each other in |
| 796 | // a specified region, propagate this knowledge recursively. |
Chris Lattner | b0dbd7f | 2002-09-06 18:41:55 +0000 | [diff] [blame] | 797 | // |
Misha Brukman | a3bbcb5 | 2002-10-29 23:06:16 +0000 | [diff] [blame] | 798 | void CEE::PropagateEquality(Value *Op0, Value *Op1, RegionInfo &RI) { |
Chris Lattner | b0dbd7f | 2002-09-06 18:41:55 +0000 | [diff] [blame] | 799 | if (Op0 == Op1) return; // Gee whiz. Are these really equal each other? |
| 800 | |
| 801 | if (isa<Constant>(Op0)) // Make sure the constant is always Op1 |
| 802 | std::swap(Op0, Op1); |
| 803 | |
| 804 | // Make sure we don't already know these are equal, to avoid infinite loops... |
| 805 | ValueInfo &VI = RI.getValueInfo(Op0); |
| 806 | |
| 807 | // Get information about the known relationship between Op0 & Op1 |
| 808 | Relation &KnownRelation = VI.getRelation(Op1); |
| 809 | |
| 810 | // If we already know they're equal, don't reprocess... |
| 811 | if (KnownRelation.getRelation() == Instruction::SetEQ) |
| 812 | return; |
| 813 | |
| 814 | // If this is boolean, check to see if one of the operands is a constant. If |
| 815 | // it's a constant, then see if the other one is one of a setcc instruction, |
| 816 | // an AND, OR, or XOR instruction. |
| 817 | // |
| 818 | if (ConstantBool *CB = dyn_cast<ConstantBool>(Op1)) { |
| 819 | |
| 820 | if (Instruction *Inst = dyn_cast<Instruction>(Op0)) { |
| 821 | // If we know that this instruction is an AND instruction, and the result |
| 822 | // is true, this means that both operands to the OR are known to be true |
| 823 | // as well. |
| 824 | // |
| 825 | if (CB->getValue() && Inst->getOpcode() == Instruction::And) { |
Misha Brukman | a3bbcb5 | 2002-10-29 23:06:16 +0000 | [diff] [blame] | 826 | PropagateEquality(Inst->getOperand(0), CB, RI); |
| 827 | PropagateEquality(Inst->getOperand(1), CB, RI); |
Chris Lattner | b0dbd7f | 2002-09-06 18:41:55 +0000 | [diff] [blame] | 828 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 829 | |
Chris Lattner | b0dbd7f | 2002-09-06 18:41:55 +0000 | [diff] [blame] | 830 | // If we know that this instruction is an OR instruction, and the result |
| 831 | // is false, this means that both operands to the OR are know to be false |
| 832 | // as well. |
| 833 | // |
| 834 | if (!CB->getValue() && Inst->getOpcode() == Instruction::Or) { |
Misha Brukman | a3bbcb5 | 2002-10-29 23:06:16 +0000 | [diff] [blame] | 835 | PropagateEquality(Inst->getOperand(0), CB, RI); |
| 836 | PropagateEquality(Inst->getOperand(1), CB, RI); |
Chris Lattner | b0dbd7f | 2002-09-06 18:41:55 +0000 | [diff] [blame] | 837 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 838 | |
Chris Lattner | b0dbd7f | 2002-09-06 18:41:55 +0000 | [diff] [blame] | 839 | // If we know that this instruction is a NOT instruction, we know that the |
| 840 | // operand is known to be the inverse of whatever the current value is. |
| 841 | // |
| 842 | if (BinaryOperator *BOp = dyn_cast<BinaryOperator>(Inst)) |
| 843 | if (BinaryOperator::isNot(BOp)) |
Misha Brukman | a3bbcb5 | 2002-10-29 23:06:16 +0000 | [diff] [blame] | 844 | PropagateEquality(BinaryOperator::getNotArgument(BOp), |
Chris Lattner | b0dbd7f | 2002-09-06 18:41:55 +0000 | [diff] [blame] | 845 | ConstantBool::get(!CB->getValue()), RI); |
| 846 | |
Misha Brukman | a3bbcb5 | 2002-10-29 23:06:16 +0000 | [diff] [blame] | 847 | // If we know the value of a SetCC instruction, propagate the information |
Chris Lattner | b0dbd7f | 2002-09-06 18:41:55 +0000 | [diff] [blame] | 848 | // about the relation into this region as well. |
| 849 | // |
| 850 | if (SetCondInst *SCI = dyn_cast<SetCondInst>(Inst)) { |
| 851 | if (CB->getValue()) { // If we know the condition is true... |
Misha Brukman | a3bbcb5 | 2002-10-29 23:06:16 +0000 | [diff] [blame] | 852 | // Propagate info about the LHS to the RHS & RHS to LHS |
| 853 | PropagateRelation(SCI->getOpcode(), SCI->getOperand(0), |
Chris Lattner | b0dbd7f | 2002-09-06 18:41:55 +0000 | [diff] [blame] | 854 | SCI->getOperand(1), RI); |
Misha Brukman | a3bbcb5 | 2002-10-29 23:06:16 +0000 | [diff] [blame] | 855 | PropagateRelation(SCI->getSwappedCondition(), |
Chris Lattner | b0dbd7f | 2002-09-06 18:41:55 +0000 | [diff] [blame] | 856 | SCI->getOperand(1), SCI->getOperand(0), RI); |
| 857 | |
| 858 | } else { // If we know the condition is false... |
| 859 | // We know the opposite of the condition is true... |
| 860 | Instruction::BinaryOps C = SCI->getInverseCondition(); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 861 | |
Misha Brukman | a3bbcb5 | 2002-10-29 23:06:16 +0000 | [diff] [blame] | 862 | PropagateRelation(C, SCI->getOperand(0), SCI->getOperand(1), RI); |
| 863 | PropagateRelation(SetCondInst::getSwappedCondition(C), |
Chris Lattner | b0dbd7f | 2002-09-06 18:41:55 +0000 | [diff] [blame] | 864 | SCI->getOperand(1), SCI->getOperand(0), RI); |
| 865 | } |
| 866 | } |
| 867 | } |
| 868 | } |
| 869 | |
Misha Brukman | a3bbcb5 | 2002-10-29 23:06:16 +0000 | [diff] [blame] | 870 | // Propagate information about Op0 to Op1 & visa versa |
| 871 | PropagateRelation(Instruction::SetEQ, Op0, Op1, RI); |
| 872 | PropagateRelation(Instruction::SetEQ, Op1, Op0, RI); |
Chris Lattner | b0dbd7f | 2002-09-06 18:41:55 +0000 | [diff] [blame] | 873 | } |
| 874 | |
| 875 | |
Misha Brukman | a3bbcb5 | 2002-10-29 23:06:16 +0000 | [diff] [blame] | 876 | // PropagateRelation - We know that the specified relation is true in all of the |
| 877 | // blocks in the specified region. Propagate the information about Op0 and |
Chris Lattner | b0dbd7f | 2002-09-06 18:41:55 +0000 | [diff] [blame] | 878 | // anything derived from it into this region. |
| 879 | // |
Misha Brukman | a3bbcb5 | 2002-10-29 23:06:16 +0000 | [diff] [blame] | 880 | void CEE::PropagateRelation(Instruction::BinaryOps Opcode, Value *Op0, |
Chris Lattner | b0dbd7f | 2002-09-06 18:41:55 +0000 | [diff] [blame] | 881 | Value *Op1, RegionInfo &RI) { |
| 882 | assert(Op0->getType() == Op1->getType() && "Equal types expected!"); |
| 883 | |
| 884 | // Constants are already pretty well understood. We will apply information |
Misha Brukman | a3bbcb5 | 2002-10-29 23:06:16 +0000 | [diff] [blame] | 885 | // about the constant to Op1 in another call to PropagateRelation. |
Chris Lattner | b0dbd7f | 2002-09-06 18:41:55 +0000 | [diff] [blame] | 886 | // |
| 887 | if (isa<Constant>(Op0)) return; |
| 888 | |
| 889 | // Get the region information for this block to update... |
| 890 | ValueInfo &VI = RI.getValueInfo(Op0); |
| 891 | |
| 892 | // Get information about the known relationship between Op0 & Op1 |
| 893 | Relation &Op1R = VI.getRelation(Op1); |
| 894 | |
| 895 | // Quick bailout for common case if we are reprocessing an instruction... |
| 896 | if (Op1R.getRelation() == Opcode) |
| 897 | return; |
| 898 | |
| 899 | // If we already have information that contradicts the current information we |
Misha Brukman | 82c89b9 | 2003-05-20 21:01:22 +0000 | [diff] [blame] | 900 | // are propagating, ignore this info. Something bad must have happened! |
Chris Lattner | b0dbd7f | 2002-09-06 18:41:55 +0000 | [diff] [blame] | 901 | // |
| 902 | if (Op1R.contradicts(Opcode, VI)) { |
| 903 | Op1R.contradicts(Opcode, VI); |
| 904 | std::cerr << "Contradiction found for opcode: " |
| 905 | << Instruction::getOpcodeName(Opcode) << "\n"; |
| 906 | Op1R.print(std::cerr); |
| 907 | return; |
| 908 | } |
| 909 | |
Misha Brukman | cf00c4a | 2003-10-10 17:57:28 +0000 | [diff] [blame] | 910 | // If the information propagated is new, then we want process the uses of this |
Misha Brukman | a3bbcb5 | 2002-10-29 23:06:16 +0000 | [diff] [blame] | 911 | // instruction to propagate the information down to them. |
Chris Lattner | b0dbd7f | 2002-09-06 18:41:55 +0000 | [diff] [blame] | 912 | // |
| 913 | if (Op1R.incorporate(Opcode, VI)) |
| 914 | UpdateUsersOfValue(Op0, RI); |
| 915 | } |
| 916 | |
| 917 | |
| 918 | // UpdateUsersOfValue - The information about V in this region has been updated. |
Misha Brukman | a3bbcb5 | 2002-10-29 23:06:16 +0000 | [diff] [blame] | 919 | // Propagate this to all consumers of the value. |
Chris Lattner | b0dbd7f | 2002-09-06 18:41:55 +0000 | [diff] [blame] | 920 | // |
| 921 | void CEE::UpdateUsersOfValue(Value *V, RegionInfo &RI) { |
| 922 | for (Value::use_iterator I = V->use_begin(), E = V->use_end(); |
| 923 | I != E; ++I) |
| 924 | if (Instruction *Inst = dyn_cast<Instruction>(*I)) { |
| 925 | // If this is an instruction using a value that we know something about, |
Misha Brukman | a3bbcb5 | 2002-10-29 23:06:16 +0000 | [diff] [blame] | 926 | // try to propagate information to the value produced by the |
Chris Lattner | b0dbd7f | 2002-09-06 18:41:55 +0000 | [diff] [blame] | 927 | // instruction. We can only do this if it is an instruction we can |
Misha Brukman | a3bbcb5 | 2002-10-29 23:06:16 +0000 | [diff] [blame] | 928 | // propagate information for (a setcc for example), and we only WANT to |
Chris Lattner | b0dbd7f | 2002-09-06 18:41:55 +0000 | [diff] [blame] | 929 | // do this if the instruction dominates this region. |
| 930 | // |
| 931 | // If the instruction doesn't dominate this region, then it cannot be |
| 932 | // used in this region and we don't care about it. If the instruction |
| 933 | // is IN this region, then we will simplify the instruction before we |
| 934 | // get to uses of it anyway, so there is no reason to bother with it |
| 935 | // here. This check is also effectively checking to make sure that Inst |
| 936 | // is in the same function as our region (in case V is a global f.e.). |
| 937 | // |
Chris Lattner | 19ef3d5 | 2006-01-11 05:09:40 +0000 | [diff] [blame] | 938 | if (EF->properlyDominates(Inst->getParent(), RI.getEntryBlock())) |
Chris Lattner | b0dbd7f | 2002-09-06 18:41:55 +0000 | [diff] [blame] | 939 | IncorporateInstruction(Inst, RI); |
| 940 | } |
| 941 | } |
| 942 | |
| 943 | // IncorporateInstruction - We just updated the information about one of the |
| 944 | // operands to the specified instruction. Update the information about the |
| 945 | // value produced by this instruction |
| 946 | // |
| 947 | void CEE::IncorporateInstruction(Instruction *Inst, RegionInfo &RI) { |
| 948 | if (SetCondInst *SCI = dyn_cast<SetCondInst>(Inst)) { |
| 949 | // See if we can figure out a result for this instruction... |
| 950 | Relation::KnownResult Result = getSetCCResult(SCI, RI); |
| 951 | if (Result != Relation::Unknown) { |
Misha Brukman | a3bbcb5 | 2002-10-29 23:06:16 +0000 | [diff] [blame] | 952 | PropagateEquality(SCI, Result ? ConstantBool::True : ConstantBool::False, |
Chris Lattner | b0dbd7f | 2002-09-06 18:41:55 +0000 | [diff] [blame] | 953 | RI); |
| 954 | } |
| 955 | } |
| 956 | } |
| 957 | |
| 958 | |
| 959 | // ComputeReplacements - Some values are known to be equal to other values in a |
| 960 | // region. For example if there is a comparison of equality between a variable |
| 961 | // X and a constant C, we can replace all uses of X with C in the region we are |
| 962 | // interested in. We generalize this replacement to replace variables with |
| 963 | // other variables if they are equal and there is a variable with lower rank |
Chris Lattner | 065a616 | 2003-09-10 05:29:43 +0000 | [diff] [blame] | 964 | // than the current one. This offers a canonicalizing property that exposes |
Chris Lattner | b0dbd7f | 2002-09-06 18:41:55 +0000 | [diff] [blame] | 965 | // more redundancies for later transformations to take advantage of. |
| 966 | // |
| 967 | void CEE::ComputeReplacements(RegionInfo &RI) { |
| 968 | // Loop over all of the values in the region info map... |
| 969 | for (RegionInfo::iterator I = RI.begin(), E = RI.end(); I != E; ++I) { |
| 970 | ValueInfo &VI = I->second; |
| 971 | |
| 972 | // If we know that this value is a particular constant, set Replacement to |
| 973 | // the constant... |
| 974 | Value *Replacement = VI.getBounds().getSingleElement(); |
| 975 | |
| 976 | // If this value is not known to be some constant, figure out the lowest |
| 977 | // rank value that it is known to be equal to (if anything). |
| 978 | // |
| 979 | if (Replacement == 0) { |
| 980 | // Find out if there are any equality relationships with values of lower |
| 981 | // rank than VI itself... |
| 982 | unsigned MinRank = getRank(I->first); |
| 983 | |
| 984 | // Loop over the relationships known about Op0. |
| 985 | const std::vector<Relation> &Relationships = VI.getRelationships(); |
| 986 | for (unsigned i = 0, e = Relationships.size(); i != e; ++i) |
| 987 | if (Relationships[i].getRelation() == Instruction::SetEQ) { |
| 988 | unsigned R = getRank(Relationships[i].getValue()); |
| 989 | if (R < MinRank) { |
| 990 | MinRank = R; |
| 991 | Replacement = Relationships[i].getValue(); |
| 992 | } |
| 993 | } |
| 994 | } |
| 995 | |
| 996 | // If we found something to replace this value with, keep track of it. |
| 997 | if (Replacement) |
| 998 | VI.setReplacement(Replacement); |
| 999 | } |
| 1000 | } |
| 1001 | |
| 1002 | // SimplifyBasicBlock - Given information about values in region RI, simplify |
| 1003 | // the instructions in the specified basic block. |
| 1004 | // |
| 1005 | bool CEE::SimplifyBasicBlock(BasicBlock &BB, const RegionInfo &RI) { |
| 1006 | bool Changed = false; |
| 1007 | for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ) { |
Chris Lattner | e408e25 | 2003-04-23 16:37:45 +0000 | [diff] [blame] | 1008 | Instruction *Inst = I++; |
Chris Lattner | b0dbd7f | 2002-09-06 18:41:55 +0000 | [diff] [blame] | 1009 | |
| 1010 | // Convert instruction arguments to canonical forms... |
| 1011 | Changed |= SimplifyInstruction(Inst, RI); |
| 1012 | |
| 1013 | if (SetCondInst *SCI = dyn_cast<SetCondInst>(Inst)) { |
| 1014 | // Try to simplify a setcc instruction based on inherited information |
| 1015 | Relation::KnownResult Result = getSetCCResult(SCI, RI); |
| 1016 | if (Result != Relation::Unknown) { |
| 1017 | DEBUG(std::cerr << "Replacing setcc with " << Result |
Chris Lattner | 2fc1230 | 2004-07-15 01:50:47 +0000 | [diff] [blame] | 1018 | << " constant: " << *SCI); |
Chris Lattner | b0dbd7f | 2002-09-06 18:41:55 +0000 | [diff] [blame] | 1019 | |
| 1020 | SCI->replaceAllUsesWith(ConstantBool::get((bool)Result)); |
| 1021 | // The instruction is now dead, remove it from the program. |
| 1022 | SCI->getParent()->getInstList().erase(SCI); |
| 1023 | ++NumSetCCRemoved; |
| 1024 | Changed = true; |
| 1025 | } |
| 1026 | } |
| 1027 | } |
| 1028 | |
| 1029 | return Changed; |
| 1030 | } |
| 1031 | |
| 1032 | // SimplifyInstruction - Inspect the operands of the instruction, converting |
Chris Lattner | 065a616 | 2003-09-10 05:29:43 +0000 | [diff] [blame] | 1033 | // them to their canonical form if possible. This takes care of, for example, |
Chris Lattner | b0dbd7f | 2002-09-06 18:41:55 +0000 | [diff] [blame] | 1034 | // replacing a value 'X' with a constant 'C' if the instruction in question is |
| 1035 | // dominated by a true seteq 'X', 'C'. |
| 1036 | // |
| 1037 | bool CEE::SimplifyInstruction(Instruction *I, const RegionInfo &RI) { |
| 1038 | bool Changed = false; |
| 1039 | |
| 1040 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) |
| 1041 | if (const ValueInfo *VI = RI.requestValueInfo(I->getOperand(i))) |
| 1042 | if (Value *Repl = VI->getReplacement()) { |
| 1043 | // If we know if a replacement with lower rank than Op0, make the |
| 1044 | // replacement now. |
Chris Lattner | 2fc1230 | 2004-07-15 01:50:47 +0000 | [diff] [blame] | 1045 | DEBUG(std::cerr << "In Inst: " << *I << " Replacing operand #" << i |
| 1046 | << " with " << *Repl << "\n"); |
Chris Lattner | b0dbd7f | 2002-09-06 18:41:55 +0000 | [diff] [blame] | 1047 | I->setOperand(i, Repl); |
| 1048 | Changed = true; |
| 1049 | ++NumOperandsCann; |
| 1050 | } |
| 1051 | |
| 1052 | return Changed; |
| 1053 | } |
| 1054 | |
| 1055 | |
Chris Lattner | f7f009d | 2002-10-08 21:34:15 +0000 | [diff] [blame] | 1056 | // getSetCCResult - Try to simplify a setcc instruction based on information |
Chris Lattner | b0dbd7f | 2002-09-06 18:41:55 +0000 | [diff] [blame] | 1057 | // inherited from a dominating setcc instruction. V is one of the operands to |
| 1058 | // the setcc instruction, and VI is the set of information known about it. We |
| 1059 | // take two cases into consideration here. If the comparison is against a |
| 1060 | // constant value, we can use the constant range to see if the comparison is |
| 1061 | // possible to succeed. If it is not a comparison against a constant, we check |
| 1062 | // to see if there is a known relationship between the two values. If so, we |
| 1063 | // may be able to eliminate the check. |
| 1064 | // |
| 1065 | Relation::KnownResult CEE::getSetCCResult(SetCondInst *SCI, |
| 1066 | const RegionInfo &RI) { |
| 1067 | Value *Op0 = SCI->getOperand(0), *Op1 = SCI->getOperand(1); |
| 1068 | Instruction::BinaryOps Opcode = SCI->getOpcode(); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1069 | |
Chris Lattner | b0dbd7f | 2002-09-06 18:41:55 +0000 | [diff] [blame] | 1070 | if (isa<Constant>(Op0)) { |
| 1071 | if (isa<Constant>(Op1)) { |
| 1072 | if (Constant *Result = ConstantFoldInstruction(SCI)) { |
| 1073 | // Wow, this is easy, directly eliminate the SetCondInst. |
Chris Lattner | 2fc1230 | 2004-07-15 01:50:47 +0000 | [diff] [blame] | 1074 | DEBUG(std::cerr << "Replacing setcc with constant fold: " << *SCI); |
Chris Lattner | b0dbd7f | 2002-09-06 18:41:55 +0000 | [diff] [blame] | 1075 | return cast<ConstantBool>(Result)->getValue() |
| 1076 | ? Relation::KnownTrue : Relation::KnownFalse; |
| 1077 | } |
| 1078 | } else { |
| 1079 | // We want to swap this instruction so that operand #0 is the constant. |
| 1080 | std::swap(Op0, Op1); |
| 1081 | Opcode = SCI->getSwappedCondition(); |
| 1082 | } |
| 1083 | } |
| 1084 | |
| 1085 | // Try to figure out what the result of this comparison will be... |
| 1086 | Relation::KnownResult Result = Relation::Unknown; |
| 1087 | |
| 1088 | // We have to know something about the relationship to prove anything... |
| 1089 | if (const ValueInfo *Op0VI = RI.requestValueInfo(Op0)) { |
| 1090 | |
| 1091 | // At this point, we know that if we have a constant argument that it is in |
| 1092 | // Op1. Check to see if we know anything about comparing value with a |
| 1093 | // constant, and if we can use this info to fold the setcc. |
| 1094 | // |
| 1095 | if (ConstantIntegral *C = dyn_cast<ConstantIntegral>(Op1)) { |
| 1096 | // Check to see if we already know the result of this comparison... |
| 1097 | ConstantRange R = ConstantRange(Opcode, C); |
| 1098 | ConstantRange Int = R.intersectWith(Op0VI->getBounds()); |
| 1099 | |
| 1100 | // If the intersection of the two ranges is empty, then the condition |
| 1101 | // could never be true! |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1102 | // |
Chris Lattner | b0dbd7f | 2002-09-06 18:41:55 +0000 | [diff] [blame] | 1103 | if (Int.isEmptySet()) { |
| 1104 | Result = Relation::KnownFalse; |
| 1105 | |
| 1106 | // Otherwise, if VI.getBounds() (the possible values) is a subset of R |
| 1107 | // (the allowed values) then we know that the condition must always be |
| 1108 | // true! |
| 1109 | // |
| 1110 | } else if (Int == Op0VI->getBounds()) { |
| 1111 | Result = Relation::KnownTrue; |
| 1112 | } |
| 1113 | } else { |
| 1114 | // If we are here, we know that the second argument is not a constant |
| 1115 | // integral. See if we know anything about Op0 & Op1 that allows us to |
| 1116 | // fold this anyway. |
| 1117 | // |
| 1118 | // Do we have value information about Op0 and a relation to Op1? |
| 1119 | if (const Relation *Op2R = Op0VI->requestRelation(Op1)) |
| 1120 | Result = Op2R->getImpliedResult(Opcode); |
| 1121 | } |
| 1122 | } |
| 1123 | return Result; |
| 1124 | } |
| 1125 | |
| 1126 | //===----------------------------------------------------------------------===// |
| 1127 | // Relation Implementation |
| 1128 | //===----------------------------------------------------------------------===// |
| 1129 | |
| 1130 | // CheckCondition - Return true if the specified condition is false. Bound may |
| 1131 | // be null. |
| 1132 | static bool CheckCondition(Constant *Bound, Constant *C, |
| 1133 | Instruction::BinaryOps BO) { |
| 1134 | assert(C != 0 && "C is not specified!"); |
| 1135 | if (Bound == 0) return false; |
| 1136 | |
Chris Lattner | 5585b33 | 2004-01-12 19:12:50 +0000 | [diff] [blame] | 1137 | Constant *Val = ConstantExpr::get(BO, Bound, C); |
| 1138 | if (ConstantBool *CB = dyn_cast<ConstantBool>(Val)) |
| 1139 | return !CB->getValue(); // Return true if the condition is false... |
| 1140 | return false; |
Chris Lattner | b0dbd7f | 2002-09-06 18:41:55 +0000 | [diff] [blame] | 1141 | } |
| 1142 | |
| 1143 | // contradicts - Return true if the relationship specified by the operand |
| 1144 | // contradicts already known information. |
| 1145 | // |
| 1146 | bool Relation::contradicts(Instruction::BinaryOps Op, |
| 1147 | const ValueInfo &VI) const { |
| 1148 | assert (Op != Instruction::Add && "Invalid relation argument!"); |
| 1149 | |
| 1150 | // If this is a relationship with a constant, make sure that this relationship |
| 1151 | // does not contradict properties known about the bounds of the constant. |
| 1152 | // |
| 1153 | if (ConstantIntegral *C = dyn_cast<ConstantIntegral>(Val)) |
| 1154 | if (ConstantRange(Op, C).intersectWith(VI.getBounds()).isEmptySet()) |
| 1155 | return true; |
| 1156 | |
| 1157 | switch (Rel) { |
| 1158 | default: assert(0 && "Unknown Relationship code!"); |
| 1159 | case Instruction::Add: return false; // Nothing known, nothing contradicts |
| 1160 | case Instruction::SetEQ: |
| 1161 | return Op == Instruction::SetLT || Op == Instruction::SetGT || |
| 1162 | Op == Instruction::SetNE; |
| 1163 | case Instruction::SetNE: return Op == Instruction::SetEQ; |
| 1164 | case Instruction::SetLE: return Op == Instruction::SetGT; |
| 1165 | case Instruction::SetGE: return Op == Instruction::SetLT; |
| 1166 | case Instruction::SetLT: |
| 1167 | return Op == Instruction::SetEQ || Op == Instruction::SetGT || |
| 1168 | Op == Instruction::SetGE; |
| 1169 | case Instruction::SetGT: |
| 1170 | return Op == Instruction::SetEQ || Op == Instruction::SetLT || |
| 1171 | Op == Instruction::SetLE; |
| 1172 | } |
| 1173 | } |
| 1174 | |
| 1175 | // incorporate - Incorporate information in the argument into this relation |
| 1176 | // entry. This assumes that the information doesn't contradict itself. If any |
| 1177 | // new information is gained, true is returned, otherwise false is returned to |
| 1178 | // indicate that nothing was updated. |
| 1179 | // |
| 1180 | bool Relation::incorporate(Instruction::BinaryOps Op, ValueInfo &VI) { |
| 1181 | assert(!contradicts(Op, VI) && |
| 1182 | "Cannot incorporate contradictory information!"); |
| 1183 | |
| 1184 | // If this is a relationship with a constant, make sure that we update the |
| 1185 | // range that is possible for the value to have... |
| 1186 | // |
| 1187 | if (ConstantIntegral *C = dyn_cast<ConstantIntegral>(Val)) |
| 1188 | VI.getBounds() = ConstantRange(Op, C).intersectWith(VI.getBounds()); |
| 1189 | |
| 1190 | switch (Rel) { |
| 1191 | default: assert(0 && "Unknown prior value!"); |
| 1192 | case Instruction::Add: Rel = Op; return true; |
| 1193 | case Instruction::SetEQ: return false; // Nothing is more precise |
| 1194 | case Instruction::SetNE: return false; // Nothing is more precise |
| 1195 | case Instruction::SetLT: return false; // Nothing is more precise |
| 1196 | case Instruction::SetGT: return false; // Nothing is more precise |
| 1197 | case Instruction::SetLE: |
| 1198 | if (Op == Instruction::SetEQ || Op == Instruction::SetLT) { |
| 1199 | Rel = Op; |
| 1200 | return true; |
| 1201 | } else if (Op == Instruction::SetNE) { |
| 1202 | Rel = Instruction::SetLT; |
| 1203 | return true; |
| 1204 | } |
| 1205 | return false; |
| 1206 | case Instruction::SetGE: return Op == Instruction::SetLT; |
| 1207 | if (Op == Instruction::SetEQ || Op == Instruction::SetGT) { |
| 1208 | Rel = Op; |
| 1209 | return true; |
| 1210 | } else if (Op == Instruction::SetNE) { |
| 1211 | Rel = Instruction::SetGT; |
| 1212 | return true; |
| 1213 | } |
| 1214 | return false; |
| 1215 | } |
| 1216 | } |
| 1217 | |
| 1218 | // getImpliedResult - If this relationship between two values implies that |
| 1219 | // the specified relationship is true or false, return that. If we cannot |
| 1220 | // determine the result required, return Unknown. |
| 1221 | // |
| 1222 | Relation::KnownResult |
| 1223 | Relation::getImpliedResult(Instruction::BinaryOps Op) const { |
| 1224 | if (Rel == Op) return KnownTrue; |
| 1225 | if (Rel == SetCondInst::getInverseCondition(Op)) return KnownFalse; |
| 1226 | |
| 1227 | switch (Rel) { |
| 1228 | default: assert(0 && "Unknown prior value!"); |
| 1229 | case Instruction::SetEQ: |
| 1230 | if (Op == Instruction::SetLE || Op == Instruction::SetGE) return KnownTrue; |
| 1231 | if (Op == Instruction::SetLT || Op == Instruction::SetGT) return KnownFalse; |
| 1232 | break; |
| 1233 | case Instruction::SetLT: |
| 1234 | if (Op == Instruction::SetNE || Op == Instruction::SetLE) return KnownTrue; |
| 1235 | if (Op == Instruction::SetEQ) return KnownFalse; |
| 1236 | break; |
| 1237 | case Instruction::SetGT: |
| 1238 | if (Op == Instruction::SetNE || Op == Instruction::SetGE) return KnownTrue; |
| 1239 | if (Op == Instruction::SetEQ) return KnownFalse; |
| 1240 | break; |
| 1241 | case Instruction::SetNE: |
| 1242 | case Instruction::SetLE: |
| 1243 | case Instruction::SetGE: |
| 1244 | case Instruction::Add: |
| 1245 | break; |
| 1246 | } |
| 1247 | return Unknown; |
| 1248 | } |
| 1249 | |
| 1250 | |
| 1251 | //===----------------------------------------------------------------------===// |
| 1252 | // Printing Support... |
| 1253 | //===----------------------------------------------------------------------===// |
| 1254 | |
| 1255 | // print - Implement the standard print form to print out analysis information. |
| 1256 | void CEE::print(std::ostream &O, const Module *M) const { |
| 1257 | O << "\nPrinting Correlated Expression Info:\n"; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1258 | for (std::map<BasicBlock*, RegionInfo>::const_iterator I = |
Chris Lattner | b0dbd7f | 2002-09-06 18:41:55 +0000 | [diff] [blame] | 1259 | RegionInfoMap.begin(), E = RegionInfoMap.end(); I != E; ++I) |
| 1260 | I->second.print(O); |
| 1261 | } |
| 1262 | |
| 1263 | // print - Output information about this region... |
| 1264 | void RegionInfo::print(std::ostream &OS) const { |
| 1265 | if (ValueMap.empty()) return; |
| 1266 | |
| 1267 | OS << " RegionInfo for basic block: " << BB->getName() << "\n"; |
| 1268 | for (std::map<Value*, ValueInfo>::const_iterator |
| 1269 | I = ValueMap.begin(), E = ValueMap.end(); I != E; ++I) |
| 1270 | I->second.print(OS, I->first); |
| 1271 | OS << "\n"; |
| 1272 | } |
| 1273 | |
| 1274 | // print - Output information about this value relation... |
| 1275 | void ValueInfo::print(std::ostream &OS, Value *V) const { |
| 1276 | if (Relationships.empty()) return; |
| 1277 | |
| 1278 | if (V) { |
| 1279 | OS << " ValueInfo for: "; |
| 1280 | WriteAsOperand(OS, V); |
| 1281 | } |
| 1282 | OS << "\n Bounds = " << Bounds << "\n"; |
| 1283 | if (Replacement) { |
| 1284 | OS << " Replacement = "; |
| 1285 | WriteAsOperand(OS, Replacement); |
| 1286 | OS << "\n"; |
| 1287 | } |
| 1288 | for (unsigned i = 0, e = Relationships.size(); i != e; ++i) |
| 1289 | Relationships[i].print(OS); |
| 1290 | } |
| 1291 | |
| 1292 | // print - Output this relation to the specified stream |
| 1293 | void Relation::print(std::ostream &OS) const { |
| 1294 | OS << " is "; |
| 1295 | switch (Rel) { |
| 1296 | default: OS << "*UNKNOWN*"; break; |
| 1297 | case Instruction::SetEQ: OS << "== "; break; |
| 1298 | case Instruction::SetNE: OS << "!= "; break; |
| 1299 | case Instruction::SetLT: OS << "< "; break; |
| 1300 | case Instruction::SetGT: OS << "> "; break; |
| 1301 | case Instruction::SetLE: OS << "<= "; break; |
| 1302 | case Instruction::SetGE: OS << ">= "; break; |
| 1303 | } |
| 1304 | |
| 1305 | WriteAsOperand(OS, Val); |
| 1306 | OS << "\n"; |
| 1307 | } |
| 1308 | |
Chris Lattner | f7f009d | 2002-10-08 21:34:15 +0000 | [diff] [blame] | 1309 | // Don't inline these methods or else we won't be able to call them from GDB! |
Chris Lattner | b0dbd7f | 2002-09-06 18:41:55 +0000 | [diff] [blame] | 1310 | void Relation::dump() const { print(std::cerr); } |
| 1311 | void ValueInfo::dump() const { print(std::cerr, 0); } |
Chris Lattner | f7f009d | 2002-10-08 21:34:15 +0000 | [diff] [blame] | 1312 | void RegionInfo::dump() const { print(std::cerr); } |