Chromium Code Reviews| Index: lib/Transforms/NaCl/SimplifyAllocas.cpp |
| diff --git a/lib/Transforms/NaCl/SimplifyAllocas.cpp b/lib/Transforms/NaCl/SimplifyAllocas.cpp |
| index ba25b5f961ad5ca28254e67de200b003bb2558e8..4ede8b5704f6b8dcb4f18035b3cae82dd42d150b 100644 |
| --- a/lib/Transforms/NaCl/SimplifyAllocas.cpp |
| +++ b/lib/Transforms/NaCl/SimplifyAllocas.cpp |
| @@ -18,7 +18,9 @@ |
| #include "llvm/IR/IntrinsicInst.h" |
| #include "llvm/IR/Module.h" |
| #include "llvm/Transforms/NaCl.h" |
| +#include "llvm/Support/Debug.h" |
| #include "llvm/Support/raw_ostream.h" |
| + |
| using namespace llvm; |
| namespace { |
| class SimplifyAllocas : public BasicBlockPass { |
| @@ -50,6 +52,21 @@ private: |
| return false; |
| } |
| + AllocaInst *findAllocaFromBC(BitCastInst *BCInst) { |
| + Value *Op0 = BCInst->getOperand(0); |
| + while (!llvm::isa<AllocaInst>(Op0)) { |
| + if (auto *NextBC = llvm::dyn_cast<BitCastInst>(Op0)) { |
| + Op0 = NextBC->getOperand(0); |
| + } else { |
| + dbgs() << "findAllocaFromBC encountered a non-bitcast intermediate val " |
| + << *Op0 << " starting w/ BCInst " << *BCInst << "\n"; |
| + llvm_unreachable("failed to find alloca"); |
| + return nullptr; |
| + } |
| + } |
| + return llvm::cast<AllocaInst>(Op0); |
| + } |
| + |
| bool runOnBasicBlock(BasicBlock &BB) override { |
| bool Changed = false; |
| for (BasicBlock::iterator I = BB.getFirstInsertionPt(), E = BB.end(); |
| @@ -106,12 +123,11 @@ private: |
| // Sometimes dbg.declare points to an argument instead of an alloca. |
| if (auto *VM = dyn_cast<ValueAsMetadata>(MV->getMetadata())) { |
| if (auto *BCInst = dyn_cast<BitCastInst>(VM->getValue())) { |
| - Value *CastSrc = BCInst->getOperand(0); |
| - assert(isa<AllocaInst>(CastSrc)); |
| + AllocaInst *Alloca = findAllocaFromBC(BCInst); |
| Call->setArgOperand( |
| 0, |
| MetadataAsValue::get(Inst->getContext(), |
| - ValueAsMetadata::get(CastSrc))); |
| + ValueAsMetadata::get(Alloca))); |
|
Derek Schuff
2015/06/11 17:43:12
what will ValueAsMetadata do if you call get on nu
jvoung (off chromium)
2015/06/11 17:52:19
It'll assert or deref null. I'll change the findAl
|
| Changed = true; |
| } |
| } |