blob: d00614b5d3dac7eda0d50f4cd671ec6a13861d68 [file] [log] [blame]
Chris Lattnerb0dbd7f2002-09-06 18:41:55 +00001//===- CorrelatedExprs.cpp - Pass to detect and eliminated c.e.'s ---------===//
Misha Brukmanfd939082005-04-21 23:48:37 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukmanfd939082005-04-21 23:48:37 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattnerb0dbd7f2002-09-06 18:41:55 +00009//
Misha Brukmana3bbcb52002-10-29 23:06:16 +000010// Correlated Expression Elimination propagates information from conditional
11// branches to blocks dominated by destinations of the branch. It propagates
Chris Lattnerb0dbd7f2002-09-06 18:41:55 +000012// 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 Brukmana3bbcb52002-10-29 23:06:16 +000016// ... 4*i; // constant propagation
Chris Lattnerb0dbd7f2002-09-06 18:41:55 +000017//
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 Lattner5585b332004-01-12 19:12:50 +000030#include "llvm/Constants.h"
Chris Lattnerb0dbd7f2002-09-06 18:41:55 +000031#include "llvm/Pass.h"
32#include "llvm/Function.h"
Chris Lattnerd23520c2003-11-10 04:10:50 +000033#include "llvm/Instructions.h"
Chris Lattner5585b332004-01-12 19:12:50 +000034#include "llvm/Type.h"
Chris Lattnerb0dbd7f2002-09-06 18:41:55 +000035#include "llvm/Analysis/Dominators.h"
Chris Lattnerd23520c2003-11-10 04:10:50 +000036#include "llvm/Assembly/Writer.h"
Chris Lattnerb0dbd7f2002-09-06 18:41:55 +000037#include "llvm/Transforms/Utils/Local.h"
Chris Lattnerd23520c2003-11-10 04:10:50 +000038#include "llvm/Transforms/Utils/BasicBlockUtils.h"
Chris Lattnerb0dbd7f2002-09-06 18:41:55 +000039#include "llvm/Support/ConstantRange.h"
40#include "llvm/Support/CFG.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000041#include "llvm/Support/Debug.h"
42#include "llvm/ADT/PostOrderIterator.h"
43#include "llvm/ADT/Statistic.h"
Chris Lattnerb0dbd7f2002-09-06 18:41:55 +000044#include <algorithm>
Chris Lattnerdac58ad2006-01-22 23:32:06 +000045#include <iostream>
Chris Lattnerd7456022004-01-09 06:02:20 +000046using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000047
Chris Lattnerb0dbd7f2002-09-06 18:41:55 +000048namespace {
Chris Lattnera92f6962002-10-01 22:38:41 +000049 Statistic<> NumSetCCRemoved("cee", "Number of setcc instruction eliminated");
Chris Lattner065a6162003-09-10 05:29:43 +000050 Statistic<> NumOperandsCann("cee", "Number of operands canonicalized");
Chris Lattnera92f6962002-10-01 22:38:41 +000051 Statistic<> BranchRevectors("cee", "Number of branches revectored");
Chris Lattnerb0dbd7f2002-09-06 18:41:55 +000052
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 Brukmana3bbcb52002-10-29 23:06:16 +0000104 // If information about this value is known or propagated from constant
Chris Lattnerb0dbd7f2002-09-06 18:41:55 +0000105 // 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 Brukmancf00c4a2003-10-10 17:57:28 +0000133 // This can invalidate references to other Relations, so use it carefully.
Chris Lattnerb0dbd7f2002-09-06 18:41:55 +0000134 //
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 Lattnerf7f009d2002-10-08 21:34:15 +0000180 // empty - return true if this region has no information known about it.
181 bool empty() const { return ValueMap.empty(); }
Misha Brukmanfd939082005-04-21 23:48:37 +0000182
Chris Lattnerb0dbd7f2002-09-06 18:41:55 +0000183 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 Lattnerf7f009d2002-10-08 21:34:15 +0000190 void dump() const;
Chris Lattnerb0dbd7f2002-09-06 18:41:55 +0000191
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 Brukmanfd939082005-04-21 23:48:37 +0000208
Chris Lattnerf7f009d2002-10-08 21:34:15 +0000209 /// 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 Lattnerb0dbd7f2002-09-06 18:41:55 +0000215 };
216
217 /// CEE - Correlated Expression Elimination
218 class CEE : public FunctionPass {
219 std::map<Value*, unsigned> RankMap;
220 std::map<BasicBlock*, RegionInfo> RegionInfoMap;
Chris Lattner19ef3d52006-01-11 05:09:40 +0000221 ETForest *EF;
Chris Lattnerb0dbd7f2002-09-06 18:41:55 +0000222 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 Lattner19ef3d52006-01-11 05:09:40 +0000228 AU.addRequired<ETForest>();
Chris Lattnerb0dbd7f2002-09-06 18:41:55 +0000229 AU.addRequired<DominatorTree>();
Chris Lattner16e7a522002-09-24 15:43:56 +0000230 AU.addRequiredID(BreakCriticalEdgesID);
Chris Lattnerb0dbd7f2002-09-06 18:41:55 +0000231 };
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 Lattnerb0dbd7f2002-09-06 18:41:55 +0000237 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 Spencer48dc46a2004-07-18 00:29:57 +0000247 if (isa<Constant>(V)) return 0;
Chris Lattnerb0dbd7f2002-09-06 18:41:55 +0000248 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 Lattnerf7f009d2002-10-08 21:34:15 +0000255 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 Brukmana3bbcb52002-10-29 23:06:16 +0000267 void PropagateBranchInfo(BranchInst *BI);
268 void PropagateEquality(Value *Op0, Value *Op1, RegionInfo &RI);
269 void PropagateRelation(Instruction::BinaryOps Opcode, Value *Op0,
Chris Lattnerb0dbd7f2002-09-06 18:41:55 +0000270 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 Brukmanfd939082005-04-21 23:48:37 +0000285 };
Chris Lattnerb0dbd7f2002-09-06 18:41:55 +0000286 RegisterOpt<CEE> X("cee", "Correlated Expression Elimination");
287}
288
Chris Lattner4b501562004-09-20 04:43:15 +0000289FunctionPass *llvm::createCorrelatedExpressionEliminationPass() {
290 return new CEE();
291}
Chris Lattnerb0dbd7f2002-09-06 18:41:55 +0000292
293
294bool 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 Lattner19ef3d52006-01-11 05:09:40 +0000301 EF = &getAnalysis<ETForest>();
Chris Lattnerb0dbd7f2002-09-06 18:41:55 +0000302 DT = &getAnalysis<DominatorTree>();
Misha Brukmanfd939082005-04-21 23:48:37 +0000303
Chris Lattnerb0dbd7f2002-09-06 18:41:55 +0000304 std::set<BasicBlock*> VisitedBlocks;
Chris Lattner02a3be02003-09-20 14:39:18 +0000305 bool Changed = TransformRegion(&F.getEntryBlock(), VisitedBlocks);
Chris Lattnerbd786962002-09-08 18:55:04 +0000306
307 RegionInfoMap.clear();
308 RankMap.clear();
309 return Changed;
Chris Lattnerb0dbd7f2002-09-06 18:41:55 +0000310}
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 Brukman82c89b92003-05-20 21:01:22 +0000315// propagating the information to successors, and finally transforming
Chris Lattnerb0dbd7f2002-09-06 18:41:55 +0000316// 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//
324bool 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 Lattnerb0dbd7f2002-09-06 18:41:55 +0000344 // 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 Brukmana3bbcb52002-10-29 23:06:16 +0000346 // blocks that are dominated by this one, we can safely propagate the
Chris Lattnerb0dbd7f2002-09-06 18:41:55 +0000347 // information down now.
348 //
349 DominatorTree::Node *BBN = (*DT)[BB];
Misha Brukmana3bbcb52002-10-29 23:06:16 +0000350 if (!RI.empty()) // Time opt: only propagate if we can change something
Chris Lattnerf7f009d2002-10-08 21:34:15 +0000351 for (unsigned i = 0, e = BBN->getChildren().size(); i != e; ++i) {
Chris Lattnerc444a422003-09-11 16:26:13 +0000352 BasicBlock *Dominated = BBN->getChildren()[i]->getBlock();
Chris Lattnerf7f009d2002-10-08 21:34:15 +0000353 assert(RegionInfoMap.find(Dominated) == RegionInfoMap.end() &&
354 "RegionInfo should be calculated in dominanace order!");
355 getRegionInfo(Dominated) = RI;
356 }
Chris Lattnerb0dbd7f2002-09-06 18:41:55 +0000357
358 // Now that all of our successors have information if they deserve it,
Misha Brukmana3bbcb52002-10-29 23:06:16 +0000359 // propagate any information our terminator instruction finds to our
Chris Lattnerb0dbd7f2002-09-06 18:41:55 +0000360 // successors.
361 if (BranchInst *BI = dyn_cast<BranchInst>(TI))
362 if (BI->isConditional())
Misha Brukmana3bbcb52002-10-29 23:06:16 +0000363 PropagateBranchInfo(BI);
Chris Lattnerb0dbd7f2002-09-06 18:41:55 +0000364
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 Lattnerc017d912002-09-23 20:06:22 +0000369 for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
Chris Lattnerf7f009d2002-10-08 21:34:15 +0000370 while (ForwardCorrelatedEdgeDestination(TI, i, RI)) {
Chris Lattnerb0dbd7f2002-09-06 18:41:55 +0000371 ++BranchRevectors;
Chris Lattnerc017d912002-09-23 20:06:22 +0000372 Changed = true;
Chris Lattnerb0dbd7f2002-09-06 18:41:55 +0000373 }
Chris Lattnerb0dbd7f2002-09-06 18:41:55 +0000374
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 Lattnerc444a422003-09-11 16:26:13 +0000377 Changed |= TransformRegion(BBN->getChildren()[i]->getBlock(),VisitedBlocks);
Chris Lattnerb0dbd7f2002-09-06 18:41:55 +0000378
Chris Lattnerb0dbd7f2002-09-06 18:41:55 +0000379 return Changed;
380}
381
Chris Lattnerf7f009d2002-10-08 21:34:15 +0000382// 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 Lattnerb0dbd7f2002-09-06 18:41:55 +0000384//
Chris Lattnerf7f009d2002-10-08 21:34:15 +0000385static 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 Lattnerfd059242003-10-15 16:48:29 +0000393 BI->getCondition()->hasOneUse()))
Chris Lattnerf7f009d2002-10-08 21:34:15 +0000394 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
418bool 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 Lattnerb0dbd7f2002-09-06 18:41:55 +0000427 // Check to see if we dominate the block. If so, this block will get the
428 // condition turned to a constant anyway.
429 //
Chris Lattner19ef3d52006-01-11 05:09:40 +0000430 //if (EF->dominates(RI.getEntryBlock(), BB))
Chris Lattnerb0dbd7f2002-09-06 18:41:55 +0000431 // return 0;
432
Chris Lattnerf7f009d2002-10-08 21:34:15 +0000433 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 Brukmanfd939082005-04-21 23:48:37 +0000462
Chris Lattnerf7f009d2002-10-08 21:34:15 +0000463 // Put the newly discovered information into the RegionInfo...
464 for (BasicBlock::iterator I = OldSucc->begin(), E = OldSucc->end(); I!=E; ++I)
Chris Lattnere408e252003-04-23 16:37:45 +0000465 if (PHINode *PN = dyn_cast<PHINode>(I)) {
Chris Lattnerf7f009d2002-10-08 21:34:15 +0000466 int OpNum = PN->getBasicBlockIndex(BB);
467 assert(OpNum != -1 && "PHI doesn't have incoming edge for predecessor!?");
Misha Brukmanfd939082005-04-21 23:48:37 +0000468 PropagateEquality(PN, PN->getIncomingValue(OpNum), NewRI);
Chris Lattnere408e252003-04-23 16:37:45 +0000469 } else if (SetCondInst *SCI = dyn_cast<SetCondInst>(I)) {
Chris Lattnerf7f009d2002-10-08 21:34:15 +0000470 Relation::KnownResult Res = getSetCCResult(SCI, NewRI);
471 if (Res == Relation::Unknown) return false;
Misha Brukmana3bbcb52002-10-29 23:06:16 +0000472 PropagateEquality(SCI, ConstantBool::get(Res), NewRI);
Chris Lattnerf7f009d2002-10-08 21:34:15 +0000473 } else {
474 assert(isa<BranchInst>(*I) && "Unexpected instruction type!");
Chris Lattnerb0dbd7f2002-09-06 18:41:55 +0000475 }
Misha Brukmanfd939082005-04-21 23:48:37 +0000476
Chris Lattnerf7f009d2002-10-08 21:34:15 +0000477 // 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 Spencer48dc46a2004-07-18 00:29:57 +0000482 isa<Constant>(PredicateVI.getReplacement()) &&
483 !isa<GlobalValue>(PredicateVI.getReplacement())) {
Chris Lattnerf7f009d2002-10-08 21:34:15 +0000484 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 Brukmanfd939082005-04-21 23:48:37 +0000490
Chris Lattnerf7f009d2002-10-08 21:34:15 +0000491 return false;
Chris Lattnerb0dbd7f2002-09-06 18:41:55 +0000492}
493
Chris Lattnerf7f009d2002-10-08 21:34:15 +0000494static 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///
505void 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 Lattner19ef3d52006-01-11 05:09:40 +0000544 if (EF->dominates(OldSucc, Dest)) {
Chris Lattnerf7f009d2002-10-08 21:34:15 +0000545 // 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 Lattner51c20e92002-11-08 23:18:37 +0000558 // There is definitely an edge from OldSucc... add the edge now
Chris Lattnerf7f009d2002-10-08 21:34:15 +0000559 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 Spencer2da5c3d2004-09-15 17:06:42 +0000578 for (BasicBlock::iterator I = OldSucc->begin(); isa<PHINode>(I); ) {
579 PHINode *PN = cast<PHINode>(I);
Chris Lattnerf7f009d2002-10-08 21:34:15 +0000580
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 Brukmanfd939082005-04-21 23:48:37 +0000586 // Make sure that anything that used to use PN now refers to EdgeValue
Chris Lattnerf7f009d2002-10-08 21:34:15 +0000587 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 Brukmanfd939082005-04-21 23:48:37 +0000607
Chris Lattnerf7f009d2002-10-08 21:34:15 +0000608 // 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 Lattnerd23520c2003-11-10 04:10:50 +0000613 SplitCriticalEdge(TI, SuccNo, this);
Chris Lattnerf7f009d2002-10-08 21:34:15 +0000614
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 Lattner19ef3d52006-01-11 05:09:40 +0000624 //EF->recalculate();
Chris Lattnerf7f009d2002-10-08 21:34:15 +0000625
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///
633void 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 Lattnerac930042005-02-01 01:23:49 +0000638 InstsToChange.reserve(Orig->getNumUses());
Chris Lattnerf7f009d2002-10-08 21:34:15 +0000639
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 Lattner19ef3d52006-01-11 05:09:40 +0000645 if (EF->dominates(RegionDominator, User->getParent()))
Chris Lattnerf7f009d2002-10-08 21:34:15 +0000646 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 Lattner19ef3d52006-01-11 05:09:40 +0000658 EF->dominates(RegionDominator, PN->getIncomingBlock(j)))
Chris Lattnerf7f009d2002-10-08 21:34:15 +0000659 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 Lattner19ef3d52006-01-11 05:09:40 +0000672 EF->dominates(RegionDominator, PN->getIncomingBlock(j)))
Chris Lattnerf7f009d2002-10-08 21:34:15 +0000673 PN->setIncomingValue(j, New);
674
675 } else {
676 InstsToChange[i]->replaceUsesOfWith(Orig, New);
677 }
678}
679
680static void CalcRegionExitBlocks(BasicBlock *Header, BasicBlock *BB,
681 std::set<BasicBlock*> &Visited,
Chris Lattner19ef3d52006-01-11 05:09:40 +0000682 ETForest &EF,
Chris Lattnerf7f009d2002-10-08 21:34:15 +0000683 std::vector<BasicBlock*> &RegionExitBlocks) {
684 if (Visited.count(BB)) return;
685 Visited.insert(BB);
686
Chris Lattner19ef3d52006-01-11 05:09:40 +0000687 if (EF.dominates(Header, BB)) { // Block in the region, recursively traverse
Chris Lattnerf7f009d2002-10-08 21:34:15 +0000688 for (succ_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I)
Chris Lattner19ef3d52006-01-11 05:09:40 +0000689 CalcRegionExitBlocks(Header, *I, Visited, EF, RegionExitBlocks);
Chris Lattnerf7f009d2002-10-08 21:34:15 +0000690 } else {
691 // Header does not dominate this block, but we have a predecessor that does
692 // dominate us. Add ourself to the list.
Misha Brukmanfd939082005-04-21 23:48:37 +0000693 RegionExitBlocks.push_back(BB);
Chris Lattnerf7f009d2002-10-08 21:34:15 +0000694 }
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///
701void 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 Lattner19ef3d52006-01-11 05:09:40 +0000706 CalcRegionExitBlocks(BB, BB, Visited, *EF, RegionExitBlocks);
Misha Brukmanfd939082005-04-21 23:48:37 +0000707
Chris Lattnerf7f009d2002-10-08 21:34:15 +0000708 // Filter out blocks that are not dominated by OldSucc...
709 for (unsigned i = 0; i != RegionExitBlocks.size(); ) {
Chris Lattner19ef3d52006-01-11 05:09:40 +0000710 if (EF->dominates(OldSucc, RegionExitBlocks[i]))
Chris Lattnerf7f009d2002-10-08 21:34:15 +0000711 ++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
720void 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 Lattner19ef3d52006-01-11 05:09:40 +0000740 NewPN->addIncoming(EF->dominates(BB, *PI) ? BBVal : OldVal, *PI);
Chris Lattnerf7f009d2002-10-08 21:34:15 +0000741 }
Misha Brukmanfd939082005-04-21 23:48:37 +0000742
Chris Lattnerf7f009d2002-10-08 21:34:15 +0000743 // Now make everyone dominated by this block use this new value!
744 ReplaceUsesOfValueInRegion(OldVal, NewPN, FBlock);
745 }
746}
747
748
749
Chris Lattnerb0dbd7f2002-09-06 18:41:55 +0000750// 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//
757void CEE::BuildRankMap(Function &F) {
758 unsigned Rank = 1; // Skip rank zero.
759
760 // Number the arguments...
Chris Lattnere4d5c442005-03-15 04:54:21 +0000761 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I)
Chris Lattnerb0dbd7f2002-09-06 18:41:55 +0000762 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 Brukmana3bbcb52002-10-29 23:06:16 +0000775// PropagateBranchInfo - When this method is invoked, we need to propagate
Chris Lattnerb0dbd7f2002-09-06 18:41:55 +0000776// 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 Brukmana3bbcb52002-10-29 23:06:16 +0000780void CEE::PropagateBranchInfo(BranchInst *BI) {
Chris Lattnerb0dbd7f2002-09-06 18:41:55 +0000781 assert(BI->isConditional() && "Must be a conditional branch!");
Chris Lattnerb0dbd7f2002-09-06 18:41:55 +0000782
Misha Brukmana3bbcb52002-10-29 23:06:16 +0000783 // Propagate information into the true block...
Chris Lattnerb0dbd7f2002-09-06 18:41:55 +0000784 //
Misha Brukmana3bbcb52002-10-29 23:06:16 +0000785 PropagateEquality(BI->getCondition(), ConstantBool::True,
Chris Lattnerf7f009d2002-10-08 21:34:15 +0000786 getRegionInfo(BI->getSuccessor(0)));
Misha Brukmanfd939082005-04-21 23:48:37 +0000787
Misha Brukmana3bbcb52002-10-29 23:06:16 +0000788 // Propagate information into the false block...
Chris Lattnerb0dbd7f2002-09-06 18:41:55 +0000789 //
Misha Brukmana3bbcb52002-10-29 23:06:16 +0000790 PropagateEquality(BI->getCondition(), ConstantBool::False,
Chris Lattnerf7f009d2002-10-08 21:34:15 +0000791 getRegionInfo(BI->getSuccessor(1)));
Chris Lattnerb0dbd7f2002-09-06 18:41:55 +0000792}
793
794
Misha Brukmana3bbcb52002-10-29 23:06:16 +0000795// PropagateEquality - If we discover that two values are equal to each other in
796// a specified region, propagate this knowledge recursively.
Chris Lattnerb0dbd7f2002-09-06 18:41:55 +0000797//
Misha Brukmana3bbcb52002-10-29 23:06:16 +0000798void CEE::PropagateEquality(Value *Op0, Value *Op1, RegionInfo &RI) {
Chris Lattnerb0dbd7f2002-09-06 18:41:55 +0000799 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 Brukmana3bbcb52002-10-29 23:06:16 +0000826 PropagateEquality(Inst->getOperand(0), CB, RI);
827 PropagateEquality(Inst->getOperand(1), CB, RI);
Chris Lattnerb0dbd7f2002-09-06 18:41:55 +0000828 }
Misha Brukmanfd939082005-04-21 23:48:37 +0000829
Chris Lattnerb0dbd7f2002-09-06 18:41:55 +0000830 // 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 Brukmana3bbcb52002-10-29 23:06:16 +0000835 PropagateEquality(Inst->getOperand(0), CB, RI);
836 PropagateEquality(Inst->getOperand(1), CB, RI);
Chris Lattnerb0dbd7f2002-09-06 18:41:55 +0000837 }
Misha Brukmanfd939082005-04-21 23:48:37 +0000838
Chris Lattnerb0dbd7f2002-09-06 18:41:55 +0000839 // 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 Brukmana3bbcb52002-10-29 23:06:16 +0000844 PropagateEquality(BinaryOperator::getNotArgument(BOp),
Chris Lattnerb0dbd7f2002-09-06 18:41:55 +0000845 ConstantBool::get(!CB->getValue()), RI);
846
Misha Brukmana3bbcb52002-10-29 23:06:16 +0000847 // If we know the value of a SetCC instruction, propagate the information
Chris Lattnerb0dbd7f2002-09-06 18:41:55 +0000848 // 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 Brukmana3bbcb52002-10-29 23:06:16 +0000852 // Propagate info about the LHS to the RHS & RHS to LHS
853 PropagateRelation(SCI->getOpcode(), SCI->getOperand(0),
Chris Lattnerb0dbd7f2002-09-06 18:41:55 +0000854 SCI->getOperand(1), RI);
Misha Brukmana3bbcb52002-10-29 23:06:16 +0000855 PropagateRelation(SCI->getSwappedCondition(),
Chris Lattnerb0dbd7f2002-09-06 18:41:55 +0000856 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 Brukmanfd939082005-04-21 23:48:37 +0000861
Misha Brukmana3bbcb52002-10-29 23:06:16 +0000862 PropagateRelation(C, SCI->getOperand(0), SCI->getOperand(1), RI);
863 PropagateRelation(SetCondInst::getSwappedCondition(C),
Chris Lattnerb0dbd7f2002-09-06 18:41:55 +0000864 SCI->getOperand(1), SCI->getOperand(0), RI);
865 }
866 }
867 }
868 }
869
Misha Brukmana3bbcb52002-10-29 23:06:16 +0000870 // Propagate information about Op0 to Op1 & visa versa
871 PropagateRelation(Instruction::SetEQ, Op0, Op1, RI);
872 PropagateRelation(Instruction::SetEQ, Op1, Op0, RI);
Chris Lattnerb0dbd7f2002-09-06 18:41:55 +0000873}
874
875
Misha Brukmana3bbcb52002-10-29 23:06:16 +0000876// 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 Lattnerb0dbd7f2002-09-06 18:41:55 +0000878// anything derived from it into this region.
879//
Misha Brukmana3bbcb52002-10-29 23:06:16 +0000880void CEE::PropagateRelation(Instruction::BinaryOps Opcode, Value *Op0,
Chris Lattnerb0dbd7f2002-09-06 18:41:55 +0000881 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 Brukmana3bbcb52002-10-29 23:06:16 +0000885 // about the constant to Op1 in another call to PropagateRelation.
Chris Lattnerb0dbd7f2002-09-06 18:41:55 +0000886 //
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 Brukman82c89b92003-05-20 21:01:22 +0000900 // are propagating, ignore this info. Something bad must have happened!
Chris Lattnerb0dbd7f2002-09-06 18:41:55 +0000901 //
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 Brukmancf00c4a2003-10-10 17:57:28 +0000910 // If the information propagated is new, then we want process the uses of this
Misha Brukmana3bbcb52002-10-29 23:06:16 +0000911 // instruction to propagate the information down to them.
Chris Lattnerb0dbd7f2002-09-06 18:41:55 +0000912 //
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 Brukmana3bbcb52002-10-29 23:06:16 +0000919// Propagate this to all consumers of the value.
Chris Lattnerb0dbd7f2002-09-06 18:41:55 +0000920//
921void 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 Brukmana3bbcb52002-10-29 23:06:16 +0000926 // try to propagate information to the value produced by the
Chris Lattnerb0dbd7f2002-09-06 18:41:55 +0000927 // instruction. We can only do this if it is an instruction we can
Misha Brukmana3bbcb52002-10-29 23:06:16 +0000928 // propagate information for (a setcc for example), and we only WANT to
Chris Lattnerb0dbd7f2002-09-06 18:41:55 +0000929 // 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 Lattner19ef3d52006-01-11 05:09:40 +0000938 if (EF->properlyDominates(Inst->getParent(), RI.getEntryBlock()))
Chris Lattnerb0dbd7f2002-09-06 18:41:55 +0000939 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//
947void 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 Brukmana3bbcb52002-10-29 23:06:16 +0000952 PropagateEquality(SCI, Result ? ConstantBool::True : ConstantBool::False,
Chris Lattnerb0dbd7f2002-09-06 18:41:55 +0000953 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 Lattner065a6162003-09-10 05:29:43 +0000964// than the current one. This offers a canonicalizing property that exposes
Chris Lattnerb0dbd7f2002-09-06 18:41:55 +0000965// more redundancies for later transformations to take advantage of.
966//
967void 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//
1005bool CEE::SimplifyBasicBlock(BasicBlock &BB, const RegionInfo &RI) {
1006 bool Changed = false;
1007 for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ) {
Chris Lattnere408e252003-04-23 16:37:45 +00001008 Instruction *Inst = I++;
Chris Lattnerb0dbd7f2002-09-06 18:41:55 +00001009
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 Lattner2fc12302004-07-15 01:50:47 +00001018 << " constant: " << *SCI);
Chris Lattnerb0dbd7f2002-09-06 18:41:55 +00001019
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 Lattner065a6162003-09-10 05:29:43 +00001033// them to their canonical form if possible. This takes care of, for example,
Chris Lattnerb0dbd7f2002-09-06 18:41:55 +00001034// replacing a value 'X' with a constant 'C' if the instruction in question is
1035// dominated by a true seteq 'X', 'C'.
1036//
1037bool 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 Lattner2fc12302004-07-15 01:50:47 +00001045 DEBUG(std::cerr << "In Inst: " << *I << " Replacing operand #" << i
1046 << " with " << *Repl << "\n");
Chris Lattnerb0dbd7f2002-09-06 18:41:55 +00001047 I->setOperand(i, Repl);
1048 Changed = true;
1049 ++NumOperandsCann;
1050 }
1051
1052 return Changed;
1053}
1054
1055
Chris Lattnerf7f009d2002-10-08 21:34:15 +00001056// getSetCCResult - Try to simplify a setcc instruction based on information
Chris Lattnerb0dbd7f2002-09-06 18:41:55 +00001057// 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//
1065Relation::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 Brukmanfd939082005-04-21 23:48:37 +00001069
Chris Lattnerb0dbd7f2002-09-06 18:41:55 +00001070 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 Lattner2fc12302004-07-15 01:50:47 +00001074 DEBUG(std::cerr << "Replacing setcc with constant fold: " << *SCI);
Chris Lattnerb0dbd7f2002-09-06 18:41:55 +00001075 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 Brukmanfd939082005-04-21 23:48:37 +00001102 //
Chris Lattnerb0dbd7f2002-09-06 18:41:55 +00001103 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.
1132static 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 Lattner5585b332004-01-12 19:12:50 +00001137 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 Lattnerb0dbd7f2002-09-06 18:41:55 +00001141}
1142
1143// contradicts - Return true if the relationship specified by the operand
1144// contradicts already known information.
1145//
1146bool 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//
1180bool 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//
1222Relation::KnownResult
1223Relation::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.
1256void CEE::print(std::ostream &O, const Module *M) const {
1257 O << "\nPrinting Correlated Expression Info:\n";
Misha Brukmanfd939082005-04-21 23:48:37 +00001258 for (std::map<BasicBlock*, RegionInfo>::const_iterator I =
Chris Lattnerb0dbd7f2002-09-06 18:41:55 +00001259 RegionInfoMap.begin(), E = RegionInfoMap.end(); I != E; ++I)
1260 I->second.print(O);
1261}
1262
1263// print - Output information about this region...
1264void 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...
1275void 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
1293void 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 Lattnerf7f009d2002-10-08 21:34:15 +00001309// Don't inline these methods or else we won't be able to call them from GDB!
Chris Lattnerb0dbd7f2002-09-06 18:41:55 +00001310void Relation::dump() const { print(std::cerr); }
1311void ValueInfo::dump() const { print(std::cerr, 0); }
Chris Lattnerf7f009d2002-10-08 21:34:15 +00001312void RegionInfo::dump() const { print(std::cerr); }