aboutsummaryrefslogtreecommitdiff
path: root/lib/Transforms/NaCl/ExpandUtils.cpp
diff options
context:
space:
mode:
authorMark Seaborn <mseaborn@chromium.org>2013-03-29 17:42:10 -0700
committerMark Seaborn <mseaborn@chromium.org>2013-03-29 17:42:10 -0700
commitcd93e1afec966dba60433f8df5f78f10ef217f93 (patch)
tree97c1b68239f23dedd72f5283e37a4957af9d10a1 /lib/Transforms/NaCl/ExpandUtils.cpp
parent946417b9fe7da9334c76182f28020ff4f46e11f8 (diff)
PNaCl: Fix ExpandTls to handle a couple of corner cases involving PHI nodes
ExpandTls's use of replaceUsesOfWith() didn't work for PHI nodes containing the same Constant twice (which needs to work for same or differing incoming BasicBlocks). The same applies to ExpandTlsConstantExpr. I noticed this while implementing ExpandConstantExpr. Fix this and factor out some common code that all three passes can use. BUG=https://code.google.com/p/nativeclient/issues/detail?id=2837 TEST=test/Transforms/NaCl/*.ll Review URL: https://codereview.chromium.org/13128002
Diffstat (limited to 'lib/Transforms/NaCl/ExpandUtils.cpp')
-rw-r--r--lib/Transforms/NaCl/ExpandUtils.cpp40
1 files changed, 40 insertions, 0 deletions
diff --git a/lib/Transforms/NaCl/ExpandUtils.cpp b/lib/Transforms/NaCl/ExpandUtils.cpp
new file mode 100644
index 0000000000..0670ff75ce
--- /dev/null
+++ b/lib/Transforms/NaCl/ExpandUtils.cpp
@@ -0,0 +1,40 @@
+//===-- ExpandUtils.cpp - Helper functions for expansion passes -----------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/IR/BasicBlock.h"
+#include "llvm/IR/Instructions.h"
+#include "llvm/Transforms/NaCl.h"
+
+using namespace llvm;
+
+Instruction *llvm::PhiSafeInsertPt(Use *U) {
+ Instruction *InsertPt = cast<Instruction>(U->getUser());
+ if (PHINode *PN = dyn_cast<PHINode>(InsertPt)) {
+ // We cannot insert instructions before a PHI node, so insert
+ // before the incoming block's terminator. This could be
+ // suboptimal if the terminator is a conditional.
+ InsertPt = PN->getIncomingBlock(*U)->getTerminator();
+ }
+ return InsertPt;
+}
+
+void llvm::PhiSafeReplaceUses(Use *U, Value *NewVal) {
+ if (PHINode *PN = dyn_cast<PHINode>(U->getUser())) {
+ // A PHI node can have multiple incoming edges from the same
+ // block, in which case all these edges must have the same
+ // incoming value.
+ BasicBlock *BB = PN->getIncomingBlock(*U);
+ for (unsigned I = 0; I < PN->getNumIncomingValues(); ++I) {
+ if (PN->getIncomingBlock(I) == BB)
+ PN->setIncomingValue(I, NewVal);
+ }
+ } else {
+ U->getUser()->replaceUsesOfWith(U->get(), NewVal);
+ }
+}