summaryrefslogtreecommitdiffstats
path: root/llvm/lib/Transforms/Scalar/BDCE.cpp
diff options
context:
space:
mode:
authorSanjay Patel <spatel@rotateright.com>2017-08-12 16:41:08 +0000
committerSanjay Patel <spatel@rotateright.com>2017-08-12 16:41:08 +0000
commitfe346f9f5be976838c05174b7c5bba17af06d4c6 (patch)
treee9e8f067e9b4b2795d62d7521878dcf4c69a34f9 /llvm/lib/Transforms/Scalar/BDCE.cpp
parentd23dd6c633b1865cfabd548099814f6943e1760e (diff)
downloadbcm5719-llvm-fe346f9f5be976838c05174b7c5bba17af06d4c6.tar.gz
bcm5719-llvm-fe346f9f5be976838c05174b7c5bba17af06d4c6.zip
[BDCE] clear poison generators after turning a value into zero (PR33695, PR34037)
nsw, nuw, and exact carry implicit assumptions about their operands, so we need to clear those after trivializing a value. We decided there was no danger for llvm.assume or metadata, so there's just a comment about that. This fixes miscompiles as shown in: https://bugs.llvm.org/show_bug.cgi?id=33695 https://bugs.llvm.org/show_bug.cgi?id=34037 Differential Revision: https://reviews.llvm.org/D36592 llvm-svn: 310779
Diffstat (limited to 'llvm/lib/Transforms/Scalar/BDCE.cpp')
-rw-r--r--llvm/lib/Transforms/Scalar/BDCE.cpp47
1 files changed, 47 insertions, 0 deletions
diff --git a/llvm/lib/Transforms/Scalar/BDCE.cpp b/llvm/lib/Transforms/Scalar/BDCE.cpp
index 61e8700f1cd..5677ac91d6f 100644
--- a/llvm/lib/Transforms/Scalar/BDCE.cpp
+++ b/llvm/lib/Transforms/Scalar/BDCE.cpp
@@ -15,6 +15,7 @@
//===----------------------------------------------------------------------===//
#include "llvm/Transforms/Scalar/BDCE.h"
+#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/Analysis/DemandedBits.h"
@@ -35,6 +36,49 @@ using namespace llvm;
STATISTIC(NumRemoved, "Number of instructions removed (unused)");
STATISTIC(NumSimplified, "Number of instructions trivialized (dead bits)");
+/// If an instruction is trivialized (dead), then the chain of users of that
+/// instruction may need to be cleared of assumptions that can no longer be
+/// guaranteed correct.
+static void clearAssumptionsOfUsers(Instruction *I, DemandedBits &DB) {
+ // Any value we're trivializing should be an integer value, and moreover,
+ // any conversion between an integer value and a non-integer value should
+ // demand all of the bits. This will cause us to stop looking down the
+ // use/def chain, so we should only see integer-typed instructions here.
+ auto isExternallyVisible = [](Instruction *I, DemandedBits &DB) {
+ assert(I->getType()->isIntegerTy() && "Trivializing a non-integer value?");
+ return DB.getDemandedBits(I).isAllOnesValue();
+ };
+
+ // Initialize the worklist with eligible direct users.
+ SmallVector<Instruction *, 16> WorkList;
+ for (User *JU : I->users()) {
+ auto *J = dyn_cast<Instruction>(JU);
+ if (J && !isExternallyVisible(J, DB))
+ WorkList.push_back(J);
+ }
+
+ // DFS through subsequent users while tracking visits to avoid cycles.
+ SmallPtrSet<Instruction *, 16> Visited;
+ while (!WorkList.empty()) {
+ Instruction *J = WorkList.pop_back_val();
+
+ // NSW, NUW, and exact are based on operands that might have changed.
+ J->dropPoisonGeneratingFlags();
+
+ // We do not have to worry about llvm.assume or range metadata:
+ // 1. llvm.assume demands its operand, so trivializing can't change it.
+ // 2. range metadata only applies to memory accesses which demand all bits.
+
+ Visited.insert(J);
+
+ for (User *KU : J->users()) {
+ auto *K = dyn_cast<Instruction>(KU);
+ if (K && !Visited.count(K) && !isExternallyVisible(K, DB))
+ WorkList.push_back(K);
+ }
+ }
+}
+
static bool bitTrackingDCE(Function &F, DemandedBits &DB) {
SmallVector<Instruction*, 128> Worklist;
bool Changed = false;
@@ -51,6 +95,9 @@ static bool bitTrackingDCE(Function &F, DemandedBits &DB) {
// replacing all uses with something else. Then, if they don't need to
// remain live (because they have side effects, etc.) we can remove them.
DEBUG(dbgs() << "BDCE: Trivializing: " << I << " (all bits dead)\n");
+
+ clearAssumptionsOfUsers(&I, DB);
+
// FIXME: In theory we could substitute undef here instead of zero.
// This should be reconsidered once we settle on the semantics of
// undef, poison, etc.
OpenPOWER on IntegriCloud