llvm.org GIT mirror llvm / cb21190
More Chris-inspired JumpThreading fixes: use ConstantExpr to correctly constant-fold undef, and be more careful with its return value. This actually exposed an infinite recursion bug in ComputeValueKnownInPredecessors which theoretically already existed (in JumpThreading's handling of and/or of i1's), but never manifested before. This patch adds a tracking set to prevent this case. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@112589 91177308-0d34-0410-b5e6-96231b3b80d8 Owen Anderson 9 years ago
2 changed file(s) with 156 addition(s) and 27 deletion(s). Raw diff Collapse all Expand all
1515 #include "llvm/IntrinsicInst.h"
1616 #include "llvm/LLVMContext.h"
1717 #include "llvm/Pass.h"
18 #include "llvm/Analysis/ConstantFolding.h"
1918 #include "llvm/Analysis/InstructionSimplify.h"
2019 #include "llvm/Analysis/LazyValueInfo.h"
2120 #include "llvm/Analysis/Loads.h"
2423 #include "llvm/Transforms/Utils/SSAUpdater.h"
2524 #include "llvm/Target/TargetData.h"
2625 #include "llvm/ADT/DenseMap.h"
26 #include "llvm/ADT/DenseSet.h"
2727 #include "llvm/ADT/Statistic.h"
2828 #include "llvm/ADT/STLExtras.h"
2929 #include "llvm/ADT/SmallPtrSet.h"
7777 #else
7878 SmallSet, 16> LoopHeaders;
7979 #endif
80 DenseSet > RecursionSet;
8081 public:
8182 static char ID; // Pass identification
8283 JumpThreading() : FunctionPass(ID) {}
269270 ///
270271 bool JumpThreading::
271272 ComputeValueKnownInPredecessors(Value *V, BasicBlock *BB,PredValueInfo &Result){
273 if (!RecursionSet.insert(std::make_pair(V, BB)).second)
274 return false;
275
272276 // If V is a constantint, then it is known in all predecessors.
273277 if (isa(V) || isa(V)) {
274278 ConstantInt *CI = dyn_cast(V);
275279
276280 for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI)
277281 Result.push_back(std::make_pair(CI, *PI));
282
283 RecursionSet.erase(std::make_pair(V, BB));
278284 return true;
279285 }
280286
309315 Result.push_back(std::make_pair(dyn_cast(PredCst), P));
310316 }
311317
318 RecursionSet.erase(std::make_pair(V, BB));
312319 return !Result.empty();
313320 }
314321
322 RecursionSet.erase(std::make_pair(V, BB));
315323 return false;
316324 }
317325
327335 PN->getIncomingBlock(i), BB);
328336 // LVI returns null is no value could be determined.
329337 if (!CI) continue;
330 ConstantInt *CInt = dyn_cast(CI);
331 Result.push_back(std::make_pair(CInt, PN->getIncomingBlock(i)));
338 if (ConstantInt *CInt = dyn_cast(CI))
339 Result.push_back(std::make_pair(CInt, PN->getIncomingBlock(i)));
340 else if (isa(CI))
341 Result.push_back(std::make_pair((ConstantInt*)0,
342 PN->getIncomingBlock(i)));
332343 }
333344 }
345
346 RecursionSet.erase(std::make_pair(V, BB));
334347 return !Result.empty();
335348 }
336349
345358 ComputeValueKnownInPredecessors(I->getOperand(0), BB, LHSVals);
346359 ComputeValueKnownInPredecessors(I->getOperand(1), BB, RHSVals);
347360
348 if (LHSVals.empty() && RHSVals.empty())
361 if (LHSVals.empty() && RHSVals.empty()) {
362 RecursionSet.erase(std::make_pair(V, BB));
349363 return false;
364 }
350365
351366 ConstantInt *InterestingVal;
352367 if (I->getOpcode() == Instruction::Or)
373388 Result.back().first = InterestingVal;
374389 }
375390 }
391
392 RecursionSet.erase(std::make_pair(V, BB));
376393 return !Result.empty();
377394 }
378395
381398 isa(I->getOperand(1)) &&
382399 cast(I->getOperand(1))->isOne()) {
383400 ComputeValueKnownInPredecessors(I->getOperand(0), BB, Result);
384 if (Result.empty())
401 if (Result.empty()) {
402 RecursionSet.erase(std::make_pair(V, BB));
385403 return false;
404 }
386405
387406 // Invert the known values.
388407 for (unsigned i = 0, e = Result.size(); i != e; ++i)
389408 if (Result[i].first)
390409 Result[i].first =
391410 cast(ConstantExpr::getNot(Result[i].first));
411
412 RecursionSet.erase(std::make_pair(V, BB));
392413 return true;
393414 }
394415
395416 // Try to simplify some other binary operator values.
396417 } else if (BinaryOperator *BO = dyn_cast(I)) {
397 // AND or OR of a value with itself is that value.
398418 ConstantInt *CI = dyn_cast(BO->getOperand(1));
399 if (CI && (BO->getOpcode() == Instruction::And ||
400 BO->getOpcode() == Instruction::Or)) {
419 if (CI) {
401420 SmallVector, 8> LHSVals;
402421 ComputeValueKnownInPredecessors(BO->getOperand(0), BB, LHSVals);
403 for (unsigned i = 0, e = LHSVals.size(); i != e; ++i)
422
423 // Try to use constant folding to simplify the binary operator.
424 for (unsigned i = 0, e = LHSVals.size(); i != e; ++i) {
425 Constant *Folded = 0;
404426 if (LHSVals[i].first == 0) {
405 ConstantInt *Zero =
406 cast(ConstantInt::get(BO->getType(), 0));
407 Result.push_back(std::make_pair(Zero, LHSVals[i].second));
408 } else if (Constant *Folded = ConstantExpr::get(BO->getOpcode(),
409 LHSVals[i].first, CI)) {
410 Result.push_back(std::make_pair(cast(Folded),
411 LHSVals[i].second));
427 Folded = ConstantExpr::get(BO->getOpcode(),
428 UndefValue::get(BO->getType()),
429 CI);
430 } else {
431 Folded = ConstantExpr::get(BO->getOpcode(), LHSVals[i].first, CI);
412432 }
413
414 return !Result.empty();
415 }
433
434 if (ConstantInt *FoldedCInt = dyn_cast(Folded))
435 Result.push_back(std::make_pair(FoldedCInt, LHSVals[i].second));
436 else if (isa(Folded))
437 Result.push_back(std::make_pair((ConstantInt*)0, LHSVals[i].second));
438 }
439 }
440
441 RecursionSet.erase(std::make_pair(V, BB));
442 return !Result.empty();
416443 }
417444
418445 // Handle compare with phi operand, where the PHI is defined in this block.
445472 Result.push_back(std::make_pair(CI, PredBB));
446473 }
447474
475 RecursionSet.erase(std::make_pair(V, BB));
448476 return !Result.empty();
449477 }
450478
471499 Result.push_back(std::make_pair(cast(ResC), P));
472500 }
473501
502 RecursionSet.erase(std::make_pair(V, BB));
474503 return !Result.empty();
475504 }
476505
477 // Try to find a constant value for the LHS of an equality comparison,
506 // Try to find a constant value for the LHS of a comparison,
478507 // and evaluate it statically if we can.
479508 if (Constant *CmpConst = dyn_cast(Cmp->getOperand(1))) {
480509 SmallVector, 8> LHSVals;
481510 ComputeValueKnownInPredecessors(I->getOperand(0), BB, LHSVals);
482511
483512 for (unsigned i = 0, e = LHSVals.size(); i != e; ++i) {
513 Constant * Folded = 0;
484514 if (LHSVals[i].first == 0)
485 Result.push_back(std::make_pair((ConstantInt*)0,
486 LHSVals[i].second));
487 else if (Constant *Folded = ConstantExpr::getCompare(
488 Cmp->getPredicate(), LHSVals[i].first, CmpConst))
489 Result.push_back(std::make_pair(cast(Folded),
490 LHSVals[i].second));
515 Folded = ConstantExpr::getCompare(Cmp->getPredicate(),
516 UndefValue::get(CmpConst->getType()), CmpConst);
517 else
518 Folded = ConstantExpr::getCompare(Cmp->getPredicate(),
519 LHSVals[i].first, CmpConst);
520
521 if (ConstantInt *FoldedCInt = dyn_cast(Folded))
522 Result.push_back(std::make_pair(FoldedCInt, LHSVals[i].second));
523 else if (isa(Folded))
524 Result.push_back(std::make_pair((ConstantInt*)0,LHSVals[i].second));
491525 }
492526
527 RecursionSet.erase(std::make_pair(V, BB));
493528 return !Result.empty();
494529 }
495530 }
504539 Result.push_back(std::make_pair(CInt, *PI));
505540 }
506541
542 RecursionSet.erase(std::make_pair(V, BB));
507543 return !Result.empty();
508544 }
509545
546 RecursionSet.erase(std::make_pair(V, BB));
510547 return false;
511548 }
512549
11251162 return false;
11261163
11271164 SmallVector, 8> PredValues;
1128 if (!ComputeValueKnownInPredecessors(Cond, BB, PredValues))
1165 if (!ComputeValueKnownInPredecessors(Cond, BB, PredValues)) {
11291166 return false;
1167 }
11301168 assert(!PredValues.empty() &&
11311169 "ComputeValueKnownInPredecessors returned true with no values");
11321170
0 ; RUN: opt < %s -jump-threading -disable-output
1 ; ModuleID = 'bugpoint-reduced-simplified.bc'
2 target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64"
3 target triple = "x86_64-apple-darwin10.4"
4
5 define void @encode_one_macroblock_highfast() nounwind ssp {
6 entry:
7 switch i32 undef, label %bb13 [
8 i32 1, label %bb10
9 i32 2, label %bb12
10 ]
11
12 bb10: ; preds = %entry
13 unreachable
14
15 bb12: ; preds = %entry
16 unreachable
17
18 bb13: ; preds = %entry
19 br i1 undef, label %bb137, label %bb292
20
21 bb137: ; preds = %bb13
22 br i1 undef, label %bb150, label %bb154
23
24 bb150: ; preds = %bb137
25 unreachable
26
27 bb154: ; preds = %bb137
28 br i1 undef, label %bb292, label %bb246
29
30 bb246: ; preds = %bb154
31 br i1 undef, label %bb292, label %bb247
32
33 bb247: ; preds = %bb246
34 br i1 undef, label %bb248, label %bb292
35
36 bb248: ; preds = %bb247
37 br i1 undef, label %bb249, label %bb292
38
39 bb249: ; preds = %bb248
40 br i1 undef, label %bb254, label %bb250
41
42 bb250: ; preds = %bb249
43 unreachable
44
45 bb254: ; preds = %bb249
46 br i1 undef, label %bb292, label %bb255
47
48 bb255: ; preds = %bb288.bb289.loopexit_crit_edge, %bb254
49 br i1 undef, label %bb.nph.split.us, label %bb269
50
51 bb.nph.split.us: ; preds = %bb255
52 br i1 undef, label %bb.nph.split.us.split.us, label %bb269.us.us31
53
54 bb.nph.split.us.split.us: ; preds = %bb.nph.split.us
55 br i1 undef, label %bb269.us.us, label %bb269.us.us.us
56
57 bb269.us.us.us: ; preds = %bb287.us.us.us, %bb.nph.split.us.split.us
58 %indvar = phi i64 [ %indvar.next, %bb287.us.us.us ], [ 0, %bb.nph.split.us.split.us ] ; [#uses=1]
59 %0 = icmp eq i16 undef, 0 ; [#uses=1]
60 br i1 %0, label %bb287.us.us.us, label %bb286.us.us.us
61
62 bb287.us.us.us: ; preds = %bb269.us.us.us
63 %indvar.next = add i64 %indvar, 1 ; [#uses=2]
64 %exitcond = icmp eq i64 %indvar.next, 4 ; [#uses=1]
65 br i1 %exitcond, label %bb288.bb289.loopexit_crit_edge, label %bb269.us.us.us
66
67 bb286.us.us.us: ; preds = %bb269.us.us.us
68 unreachable
69
70 bb269.us.us: ; preds = %bb287.us.us, %bb.nph.split.us.split.us
71 br i1 undef, label %bb287.us.us, label %bb286.us.us
72
73 bb287.us.us: ; preds = %bb269.us.us
74 br i1 undef, label %bb288.bb289.loopexit_crit_edge, label %bb269.us.us
75
76 bb286.us.us: ; preds = %bb269.us.us
77 unreachable
78
79 bb269.us.us31: ; preds = %bb.nph.split.us
80 unreachable
81
82 bb269: ; preds = %bb255
83 unreachable
84
85 bb288.bb289.loopexit_crit_edge: ; preds = %bb287.us.us, %bb287.us.us.us
86 br i1 undef, label %bb292, label %bb255
87
88 bb292: ; preds = %bb288.bb289.loopexit_crit_edge, %bb254, %bb248, %bb247, %bb246, %bb154, %bb13
89 unreachable
90 }