llvm.org GIT mirror llvm / 6033b34
Implement jump threading of 'indirectbr' by keeping track of whether we're looking for ConstantInt*s or BlockAddress*s. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@121066 91177308-0d34-0410-b5e6-96231b3b80d8 Frits van Bommel 8 years ago
2 changed file(s) with 141 addition(s) and 46 deletion(s). Raw diff Collapse all Expand all
4747 // These are at global scope so static functions can use them too.
4848 typedef SmallVectorImpl > PredValueInfo;
4949 typedef SmallVector, 8> PredValueInfoTy;
50
51 // This is used to keep track of what kind of constant we're currently hoping
52 // to find.
53 enum ConstantPreference {
54 WantInteger,
55 WantBlockAddress
56 };
5057
5158 /// This pass performs 'jump threading', which looks at blocks that have
5259 /// multiple predecessors and multiple successors. If one or more of the
108115 const SmallVectorImpl &PredBBs);
109116
110117 bool ComputeValueKnownInPredecessors(Value *V, BasicBlock *BB,
111 PredValueInfo &Result);
112 bool ProcessThreadableEdges(Value *Cond, BasicBlock *BB);
118 PredValueInfo &Result,
119 ConstantPreference Preference);
120 bool ProcessThreadableEdges(Value *Cond, BasicBlock *BB,
121 ConstantPreference Preference);
113122
114123
115124 bool ProcessBranchOnDuplicateCond(BasicBlock *PredBB, BasicBlock *DestBB);
246255 if (isa(I))
247256 Size = Size > 6 ? Size-6 : 0;
248257
258 // The same holds for indirect branches, but slightly more so.
259 if (isa(I))
260 Size = Size > 8 ? Size-8 : 0;
261
249262 return Size;
250263 }
251264
274287
275288 /// getKnownConstant - Helper method to determine if we can thread over a
276289 /// terminator with the given value as its condition, and if so what value to
277 /// use for that.
290 /// use for that. What kind of value this is depends on whether we want an
291 /// integer or a block address, but an undef is always accepted.
278292 /// Returns null if Val is null or not an appropriate constant.
279 static Constant *getKnownConstant(Value *Val) {
293 static Constant *getKnownConstant(Value *Val, ConstantPreference Preference) {
280294 if (!Val)
281295 return 0;
282296
284298 if (UndefValue *U = dyn_cast(Val))
285299 return U;
286300
301 if (Preference == WantBlockAddress)
302 return dyn_cast(Val->stripPointerCasts());
303
287304 return dyn_cast(Val);
288305 }
289306
290 // Helper method for ComputeValueKnownInPredecessors. If Value is a
291 // ConstantInt or undef, push it. Otherwise, do nothing.
292 static void PushKnownConstantOrUndef(PredValueInfo &Result, Constant *Value,
293 BasicBlock *BB) {
294 if (Constant *KC = getKnownConstant(Value))
295 Result.push_back(std::make_pair(KC, BB));
296 }
297
298307 /// ComputeValueKnownInPredecessors - Given a basic block BB and a value V, see
299 /// if we can infer that the value is a known ConstantInt in any of our
300 /// predecessors. If so, return the known list of value and pred BB in the
301 /// result vector. If a value is known to be undef, it is returned as null.
308 /// if we can infer that the value is a known ConstantInt/BlockAddress or undef
309 /// in any of our predecessors. If so, return the known list of value and pred
310 /// BB in the result vector.
302311 ///
303312 /// This returns true if there were any known values.
304313 ///
305314 bool JumpThreading::
306 ComputeValueKnownInPredecessors(Value *V, BasicBlock *BB,PredValueInfo &Result){
315 ComputeValueKnownInPredecessors(Value *V, BasicBlock *BB, PredValueInfo &Result,
316 ConstantPreference Preference) {
307317 // This method walks up use-def chains recursively. Because of this, we could
308318 // get into an infinite loop going around loops in the use-def chain. To
309319 // prevent this, keep track of what (value, block) pairs we've already visited
316326 RecursionSetRemover remover(RecursionSet, std::make_pair(V, BB));
317327
318328 // If V is a constant, then it is known in all predecessors.
319 if (Constant *KC = getKnownConstant(V)) {
329 if (Constant *KC = getKnownConstant(V, Preference)) {
320330 for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI)
321331 Result.push_back(std::make_pair(KC, *PI));
322332
346356 // If the value is known by LazyValueInfo to be a constant in a
347357 // predecessor, use that information to try to thread this block.
348358 Constant *PredCst = LVI->getConstantOnEdge(V, P, BB);
349 if (Constant *KC = getKnownConstant(PredCst))
359 if (Constant *KC = getKnownConstant(PredCst, Preference))
350360 Result.push_back(std::make_pair(KC, P));
351361 }
352362
357367 if (PHINode *PN = dyn_cast(I)) {
358368 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
359369 Value *InVal = PN->getIncomingValue(i);
360 if (Constant *KC = getKnownConstant(InVal)) {
370 if (Constant *KC = getKnownConstant(InVal, Preference)) {
361371 Result.push_back(std::make_pair(KC, PN->getIncomingBlock(i)));
362372 } else {
363373 Constant *CI = LVI->getConstantOnEdge(InVal,
364374 PN->getIncomingBlock(i), BB);
365 // LVI returns null is no value could be determined.
366 if (!CI) continue;
367 PushKnownConstantOrUndef(Result, CI, PN->getIncomingBlock(i));
375 if (Constant *KC = getKnownConstant(CI, Preference))
376 Result.push_back(std::make_pair(KC, PN->getIncomingBlock(i)));
368377 }
369378 }
370379
375384
376385 // Handle some boolean conditions.
377386 if (I->getType()->getPrimitiveSizeInBits() == 1) {
387 assert(Preference == WantInteger && "One-bit non-integer type?");
378388 // X | true -> true
379389 // X & false -> false
380390 if (I->getOpcode() == Instruction::Or ||
381391 I->getOpcode() == Instruction::And) {
382 ComputeValueKnownInPredecessors(I->getOperand(0), BB, LHSVals);
383 ComputeValueKnownInPredecessors(I->getOperand(1), BB, RHSVals);
392 ComputeValueKnownInPredecessors(I->getOperand(0), BB, LHSVals,
393 WantInteger);
394 ComputeValueKnownInPredecessors(I->getOperand(1), BB, RHSVals,
395 WantInteger);
384396
385397 if (LHSVals.empty() && RHSVals.empty())
386398 return false;
420432 if (I->getOpcode() == Instruction::Xor &&
421433 isa(I->getOperand(1)) &&
422434 cast(I->getOperand(1))->isOne()) {
423 ComputeValueKnownInPredecessors(I->getOperand(0), BB, Result);
435 ComputeValueKnownInPredecessors(I->getOperand(0), BB, Result,
436 WantInteger);
424437 if (Result.empty())
425438 return false;
426439
433446
434447 // Try to simplify some other binary operator values.
435448 } else if (BinaryOperator *BO = dyn_cast(I)) {
449 assert(Preference != WantBlockAddress
450 && "A binary operator creating a block address?");
436451 if (ConstantInt *CI = dyn_cast(BO->getOperand(1))) {
437452 PredValueInfoTy LHSVals;
438 ComputeValueKnownInPredecessors(BO->getOperand(0), BB, LHSVals);
453 ComputeValueKnownInPredecessors(BO->getOperand(0), BB, LHSVals,
454 WantInteger);
439455
440456 // Try to use constant folding to simplify the binary operator.
441457 for (unsigned i = 0, e = LHSVals.size(); i != e; ++i) {
442458 Constant *V = LHSVals[i].first;
443459 Constant *Folded = ConstantExpr::get(BO->getOpcode(), V, CI);
444460
445 PushKnownConstantOrUndef(Result, Folded, LHSVals[i].second);
461 if (Constant *KC = getKnownConstant(Folded, WantInteger))
462 Result.push_back(std::make_pair(KC, LHSVals[i].second));
446463 }
447464 }
448465
451468
452469 // Handle compare with phi operand, where the PHI is defined in this block.
453470 if (CmpInst *Cmp = dyn_cast(I)) {
471 assert(Preference == WantInteger && "Compares only produce integers");
454472 PHINode *PN = dyn_cast(Cmp->getOperand(0));
455473 if (PN && PN->getParent() == BB) {
456474 // We can do this simplification if any comparisons fold to true or false.
473491 Res = ConstantInt::get(Type::getInt1Ty(LHS->getContext()), ResT);
474492 }
475493
476 if (Constant *ConstRes = dyn_cast(Res))
477 PushKnownConstantOrUndef(Result, ConstRes, PredBB);
494 if (Constant *KC = getKnownConstant(Res, WantInteger))
495 Result.push_back(std::make_pair(KC, PredBB));
478496 }
479497
480498 return !Result.empty();
509527 // and evaluate it statically if we can.
510528 if (Constant *CmpConst = dyn_cast(Cmp->getOperand(1))) {
511529 PredValueInfoTy LHSVals;
512 ComputeValueKnownInPredecessors(I->getOperand(0), BB, LHSVals);
530 ComputeValueKnownInPredecessors(I->getOperand(0), BB, LHSVals,
531 WantInteger);
513532
514533 for (unsigned i = 0, e = LHSVals.size(); i != e; ++i) {
515534 Constant *V = LHSVals[i].first;
516535 Constant *Folded = ConstantExpr::getCompare(Cmp->getPredicate(),
517536 V, CmpConst);
518 PushKnownConstantOrUndef(Result, Folded, LHSVals[i].second);
537 if (Constant *KC = getKnownConstant(Folded, WantInteger))
538 Result.push_back(std::make_pair(KC, LHSVals[i].second));
519539 }
520540
521541 return !Result.empty();
525545
526546 // If all else fails, see if LVI can figure out a constant value for us.
527547 Constant *CI = LVI->getConstant(V, BB);
528 if (Constant *KC = getKnownConstant(CI)) {
548 if (Constant *KC = getKnownConstant(CI, Preference)) {
529549 for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI)
530550 Result.push_back(std::make_pair(KC, *PI));
531551 }
589609 }
590610 }
591611
592 // Look to see if the terminator is a branch of switch, if not we can't thread
593 // it.
612 // What kind of constant we're looking for.
613 ConstantPreference Preference = WantInteger;
614
615 // Look to see if the terminator is a conditional branch, switch or indirect
616 // branch, if not we can't thread it.
594617 Value *Condition;
595 if (BranchInst *BI = dyn_cast(BB->getTerminator())) {
618 Instruction *Terminator = BB->getTerminator();
619 if (BranchInst *BI = dyn_cast(Terminator)) {
596620 // Can't thread an unconditional jump.
597621 if (BI->isUnconditional()) return false;
598622 Condition = BI->getCondition();
599 } else if (SwitchInst *SI = dyn_cast(BB->getTerminator()))
623 } else if (SwitchInst *SI = dyn_cast(Terminator)) {
600624 Condition = SI->getCondition();
601 else
625 } else if (IndirectBrInst *IB = dyn_cast(Terminator)) {
626 Condition = IB->getAddress()->stripPointerCasts();
627 Preference = WantBlockAddress;
628 } else {
602629 return false; // Must be an invoke.
630 }
603631
604632 // If the terminator is branching on an undef, we can pick any of the
605633 // successors to branch to. Let GetBestDestForJumpOnUndef decide.
623651 // If the terminator of this block is branching on a constant, simplify the
624652 // terminator to an unconditional branch. This can occur due to threading in
625653 // other blocks.
626 if (getKnownConstant(Condition)) {
654 if (getKnownConstant(Condition, Preference)) {
627655 DEBUG(dbgs() << " In block '" << BB->getName()
628656 << "' folding terminator: " << *BB->getTerminator() << '\n');
629657 ++NumFolds;
636664 // All the rest of our checks depend on the condition being an instruction.
637665 if (CondInst == 0) {
638666 // FIXME: Unify this with code below.
639 if (ProcessThreadableEdges(Condition, BB))
667 if (ProcessThreadableEdges(Condition, BB, Preference))
640668 return true;
641669 return false;
642670 }
702730 // a PHI node in the current block. If we can prove that any predecessors
703731 // compute a predictable value based on a PHI node, thread those predecessors.
704732 //
705 if (ProcessThreadableEdges(CondInst, BB))
733 if (ProcessThreadableEdges(CondInst, BB, Preference))
706734 return true;
707735
708736 // If this is an otherwise-unfoldable branch on a phi node in the current
10871115 return MostPopularDest;
10881116 }
10891117
1090 bool JumpThreading::ProcessThreadableEdges(Value *Cond, BasicBlock *BB) {
1118 bool JumpThreading::ProcessThreadableEdges(Value *Cond, BasicBlock *BB,
1119 ConstantPreference Preference) {
10911120 // If threading this would thread across a loop header, don't even try to
10921121 // thread the edge.
10931122 if (LoopHeaders.count(BB))
10941123 return false;
10951124
10961125 PredValueInfoTy PredValues;
1097 if (!ComputeValueKnownInPredecessors(Cond, BB, PredValues))
1126 if (!ComputeValueKnownInPredecessors(Cond, BB, PredValues, Preference))
10981127 return false;
10991128
11001129 assert(!PredValues.empty() &&
11341163 DestBB = 0;
11351164 else if (BranchInst *BI = dyn_cast(BB->getTerminator()))
11361165 DestBB = BI->getSuccessor(cast(Val)->isZero());
1166 else if (SwitchInst *SI = dyn_cast(BB->getTerminator()))
1167 DestBB = SI->getSuccessor(SI->findCaseValue(cast(Val)));
11371168 else {
1138 SwitchInst *SI = cast(BB->getTerminator());
1139 DestBB = SI->getSuccessor(SI->findCaseValue(cast(Val)));
1169 assert(isa(BB->getTerminator())
1170 && "Unexpected terminator");
1171 DestBB = cast(Val)->getBasicBlock();
11401172 }
11411173
11421174 // If we have exactly one destination, remember it for efficiency below.
12551287
12561288 PredValueInfoTy XorOpValues;
12571289 bool isLHS = true;
1258 if (!ComputeValueKnownInPredecessors(BO->getOperand(0), BB, XorOpValues)) {
1290 if (!ComputeValueKnownInPredecessors(BO->getOperand(0), BB, XorOpValues,
1291 WantInteger)) {
12591292 assert(XorOpValues.empty());
1260 if (!ComputeValueKnownInPredecessors(BO->getOperand(1), BB, XorOpValues))
1293 if (!ComputeValueKnownInPredecessors(BO->getOperand(1), BB, XorOpValues,
1294 WantInteger))
12611295 return false;
12621296 isLHS = false;
12631297 }
0 ; RUN: opt -S < %s -jump-threading | FileCheck %s
1
2 ; Keep block addresses alive.
3 @addresses = constant [4 x i8*] [
4 i8* blockaddress(@test1, %L1), i8* blockaddress(@test1, %L2),
5 i8* blockaddress(@test2, %L1), i8* blockaddress(@test2, %L2)
6 ]
7
8 declare void @bar()
9 declare void @baz()
10
11
12
13 ; Check basic jump threading for indirectbr instructions.
14
15 ; CHECK: void @test1
16 ; CHECK: br i1 %tobool, label %L1, label %indirectgoto
17 ; CHECK-NOT: if.else:
18 ; CHECK: L1:
19 ; CHECK: indirectbr i8* %address, [label %L1, label %L2]
20 define void @test1(i32 %i, i8* %address) nounwind {
21 entry:
22 %rem = srem i32 %i, 2
23 %tobool = icmp ne i32 %rem, 0
24 br i1 %tobool, label %indirectgoto, label %if.else
25
26 if.else: ; preds = %entry
27 br label %indirectgoto
28
29 L1: ; preds = %indirectgoto
30 call void @bar()
31 ret void
32
33 L2: ; preds = %indirectgoto
34 call void @baz()
35 ret void
36
37 indirectgoto: ; preds = %if.else, %entry
38 %indirect.goto.dest = phi i8* [ %address, %if.else ], [ blockaddress(@test1, %L1), %entry ]
39 indirectbr i8* %indirect.goto.dest, [label %L1, label %L2]
40 }
41
42
43 ; Check constant folding of indirectbr
44
45 ; CHECK: void @test2
46 ; CHECK-NEXT: :
47 ; CHECK-NEXT: call void @bar
48 ; CHECK-NEXT: ret void
49 define void @test2() nounwind {
50 entry:
51 indirectbr i8* blockaddress(@test2, %L1), [label %L1, label %L2]
52
53 L1: ; preds = %indirectgoto
54 call void @bar()
55 ret void
56
57 L2: ; preds = %indirectgoto
58 call void @baz()
59 ret void
60 }