13bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien//===- BitcodeReader.cpp - Internal BitcodeReader implementation ----------===//
23bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien//
33bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien//                     The LLVM Compiler Infrastructure
43bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien//
53bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien// This file is distributed under the University of Illinois Open Source
63bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien// License. See LICENSE.TXT for details.
73bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien//
83bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien//===----------------------------------------------------------------------===//
93bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien//
103bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien// This header defines the BitcodeReader class.
113bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien//
123bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien//===----------------------------------------------------------------------===//
133bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien
143bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien#include "llvm/Bitcode/ReaderWriter.h"
15c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien#include "BitReader_3_0.h"
165cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines#include "llvm/ADT/STLExtras.h"
173bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien#include "llvm/ADT/SmallString.h"
183bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien#include "llvm/ADT/SmallVector.h"
19c2074caf075818abb6d3689ad924ca09f4a5ba1fTim Murray#include "llvm/IR/AutoUpgrade.h"
20b730e239619a546d93e5926ea92d698ab77ec7f6Stephen Hines#include "llvm/IR/Constants.h"
211bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines#include "llvm/IR/CFG.h"
22b730e239619a546d93e5926ea92d698ab77ec7f6Stephen Hines#include "llvm/IR/DerivedTypes.h"
231bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines#include "llvm/IR/DiagnosticPrinter.h"
245cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines#include "llvm/IR/GVMaterializer.h"
25b730e239619a546d93e5926ea92d698ab77ec7f6Stephen Hines#include "llvm/IR/InlineAsm.h"
26b730e239619a546d93e5926ea92d698ab77ec7f6Stephen Hines#include "llvm/IR/IntrinsicInst.h"
27b730e239619a546d93e5926ea92d698ab77ec7f6Stephen Hines#include "llvm/IR/IRBuilder.h"
281bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines#include "llvm/IR/LLVMContext.h"
29b730e239619a546d93e5926ea92d698ab77ec7f6Stephen Hines#include "llvm/IR/Module.h"
30b730e239619a546d93e5926ea92d698ab77ec7f6Stephen Hines#include "llvm/IR/OperandTraits.h"
31b730e239619a546d93e5926ea92d698ab77ec7f6Stephen Hines#include "llvm/IR/Operator.h"
32b730e239619a546d93e5926ea92d698ab77ec7f6Stephen Hines#include "llvm/ADT/SmallPtrSet.h"
331bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines#include "llvm/Support/ManagedStatic.h"
343bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien#include "llvm/Support/MathExtras.h"
353bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien#include "llvm/Support/MemoryBuffer.h"
361bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines
373bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chienusing namespace llvm;
38c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chienusing namespace llvm_3_0;
39c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien
40c73b5214fa71ef6e3378fa121bb8b6312d2e6d3bShih-wei Liao#define FUNC_CODE_INST_UNWIND_2_7     14
41c73b5214fa71ef6e3378fa121bb8b6312d2e6d3bShih-wei Liao#define eh_exception_2_7             145
42c73b5214fa71ef6e3378fa121bb8b6312d2e6d3bShih-wei Liao#define eh_selector_2_7              149
43c73b5214fa71ef6e3378fa121bb8b6312d2e6d3bShih-wei Liao
44c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien#define TYPE_BLOCK_ID_OLD_3_0         10
45c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien#define TYPE_SYMTAB_BLOCK_ID_OLD_3_0  13
46c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien#define TYPE_CODE_STRUCT_OLD_3_0      10
47c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien
48c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chiennamespace {
49c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien  void FindExnAndSelIntrinsics(BasicBlock *BB, CallInst *&Exn,
50c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien                                      CallInst *&Sel,
51c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien                                      SmallPtrSet<BasicBlock*, 8> &Visited) {
52579361346abc6696c805e3904a18178ebce4e4a3Stephen Hines    if (!Visited.insert(BB).second) return;
53c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien
54c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien    for (BasicBlock::iterator
55c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien           I = BB->begin(), E = BB->end(); I != E; ++I) {
56c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien      if (CallInst *CI = dyn_cast<CallInst>(I)) {
57c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien        switch (CI->getCalledFunction()->getIntrinsicID()) {
58c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien        default: break;
59c73b5214fa71ef6e3378fa121bb8b6312d2e6d3bShih-wei Liao        case eh_exception_2_7:
60c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien          assert(!Exn && "Found more than one eh.exception call!");
61c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien          Exn = CI;
62c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien          break;
63c73b5214fa71ef6e3378fa121bb8b6312d2e6d3bShih-wei Liao        case eh_selector_2_7:
64c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien          assert(!Sel && "Found more than one eh.selector call!");
65c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien          Sel = CI;
66c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien          break;
67c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien        }
68c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien
69c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien        if (Exn && Sel) return;
70c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien      }
71c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien    }
72c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien
73c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien    if (Exn && Sel) return;
74c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien
75c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien    for (succ_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) {
76c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien      FindExnAndSelIntrinsics(*I, Exn, Sel, Visited);
77c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien      if (Exn && Sel) return;
78c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien    }
79c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien  }
80c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien
81c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien
82c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien
83c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien  /// TransferClausesToLandingPadInst - Transfer the exception handling clauses
84c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien  /// from the eh_selector call to the new landingpad instruction.
85c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien  void TransferClausesToLandingPadInst(LandingPadInst *LPI,
86c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien                                              CallInst *EHSel) {
87c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien    LLVMContext &Context = LPI->getContext();
88c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien    unsigned N = EHSel->getNumArgOperands();
89c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien
90c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien    for (unsigned i = N - 1; i > 1; --i) {
91c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien      if (const ConstantInt *CI = dyn_cast<ConstantInt>(EHSel->getArgOperand(i))){
92c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien        unsigned FilterLength = CI->getZExtValue();
93c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien        unsigned FirstCatch = i + FilterLength + !FilterLength;
94c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien        assert(FirstCatch <= N && "Invalid filter length");
95c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien
96c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien        if (FirstCatch < N)
97c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien          for (unsigned j = FirstCatch; j < N; ++j) {
98c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien            Value *Val = EHSel->getArgOperand(j);
99c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien            if (!Val->hasName() || Val->getName() != "llvm.eh.catch.all.value") {
100d0993af5b1337f8186dd9aedea2e138a919e02e9Stephen Hines              LPI->addClause(cast<Constant>(EHSel->getArgOperand(j)));
101c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien            } else {
102c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien              GlobalVariable *GV = cast<GlobalVariable>(Val);
103c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien              LPI->addClause(GV->getInitializer());
104c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien            }
105c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien          }
106c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien
107c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien        if (!FilterLength) {
108c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien          // Cleanup.
109c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien          LPI->setCleanup(true);
110c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien        } else {
111c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien          // Filter.
112c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien          SmallVector<Constant *, 4> TyInfo;
113c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien          TyInfo.reserve(FilterLength - 1);
114c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien          for (unsigned j = i + 1; j < FirstCatch; ++j)
115c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien            TyInfo.push_back(cast<Constant>(EHSel->getArgOperand(j)));
116c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien          ArrayType *AType =
117c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien            ArrayType::get(!TyInfo.empty() ? TyInfo[0]->getType() :
118c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien                           PointerType::getUnqual(Type::getInt8Ty(Context)),
119c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien                           TyInfo.size());
120c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien          LPI->addClause(ConstantArray::get(AType, TyInfo));
121c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien        }
122c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien
123c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien        N = i;
124c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien      }
125c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien    }
126c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien
127c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien    if (N > 2)
128c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien      for (unsigned j = 2; j < N; ++j) {
129c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien        Value *Val = EHSel->getArgOperand(j);
130c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien        if (!Val->hasName() || Val->getName() != "llvm.eh.catch.all.value") {
131d0993af5b1337f8186dd9aedea2e138a919e02e9Stephen Hines          LPI->addClause(cast<Constant>(EHSel->getArgOperand(j)));
132c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien        } else {
133c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien          GlobalVariable *GV = cast<GlobalVariable>(Val);
134c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien          LPI->addClause(GV->getInitializer());
135c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien        }
136c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien      }
137c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien  }
138c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien
139c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien
140c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien  /// This function upgrades the old pre-3.0 exception handling system to the new
141c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien  /// one. N.B. This will be removed in 3.1.
142c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien  void UpgradeExceptionHandling(Module *M) {
143c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien    Function *EHException = M->getFunction("llvm.eh.exception");
144c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien    Function *EHSelector = M->getFunction("llvm.eh.selector");
145c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien    if (!EHException || !EHSelector)
146c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien      return;
147c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien
148c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien    LLVMContext &Context = M->getContext();
149c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien    Type *ExnTy = PointerType::getUnqual(Type::getInt8Ty(Context));
150c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien    Type *SelTy = Type::getInt32Ty(Context);
151900c6c1f08f7c572125d7d39abe0f0f9eafbfa14Chris Wailes    Type *LPadSlotTy = StructType::get(ExnTy, SelTy, nullptr);
152c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien
153c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien    // This map links the invoke instruction with the eh.exception and eh.selector
154c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien    // calls associated with it.
155c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien    DenseMap<InvokeInst*, std::pair<Value*, Value*> > InvokeToIntrinsicsMap;
156c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien    for (Module::iterator
157c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien           I = M->begin(), E = M->end(); I != E; ++I) {
158c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien      Function &F = *I;
159c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien
160c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien      for (Function::iterator
161c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien             II = F.begin(), IE = F.end(); II != IE; ++II) {
162c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien        BasicBlock *BB = &*II;
163c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien        InvokeInst *Inst = dyn_cast<InvokeInst>(BB->getTerminator());
164c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien        if (!Inst) continue;
165c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien        BasicBlock *UnwindDest = Inst->getUnwindDest();
166c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien        if (UnwindDest->isLandingPad()) continue; // Already converted.
167c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien
168c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien        SmallPtrSet<BasicBlock*, 8> Visited;
169c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien        CallInst *Exn = 0;
170c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien        CallInst *Sel = 0;
171c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien        FindExnAndSelIntrinsics(UnwindDest, Exn, Sel, Visited);
172c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien        assert(Exn && Sel && "Cannot find eh.exception and eh.selector calls!");
173c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien        InvokeToIntrinsicsMap[Inst] = std::make_pair(Exn, Sel);
174c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien      }
175c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien    }
176c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien
177c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien    // This map stores the slots where the exception object and selector value are
178c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien    // stored within a function.
179c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien    DenseMap<Function*, std::pair<Value*, Value*> > FnToLPadSlotMap;
180c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien    SmallPtrSet<Instruction*, 32> DeadInsts;
181c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien    for (DenseMap<InvokeInst*, std::pair<Value*, Value*> >::iterator
182c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien           I = InvokeToIntrinsicsMap.begin(), E = InvokeToIntrinsicsMap.end();
183c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien         I != E; ++I) {
184c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien      InvokeInst *Invoke = I->first;
185c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien      BasicBlock *UnwindDest = Invoke->getUnwindDest();
186c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien      Function *F = UnwindDest->getParent();
187c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien      std::pair<Value*, Value*> EHIntrinsics = I->second;
188c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien      CallInst *Exn = cast<CallInst>(EHIntrinsics.first);
189c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien      CallInst *Sel = cast<CallInst>(EHIntrinsics.second);
190c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien
191c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien      // Store the exception object and selector value in the entry block.
192c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien      Value *ExnSlot = 0;
193c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien      Value *SelSlot = 0;
194c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien      if (!FnToLPadSlotMap[F].first) {
195c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien        BasicBlock *Entry = &F->front();
196c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien        ExnSlot = new AllocaInst(ExnTy, "exn", Entry->getTerminator());
197c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien        SelSlot = new AllocaInst(SelTy, "sel", Entry->getTerminator());
198c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien        FnToLPadSlotMap[F] = std::make_pair(ExnSlot, SelSlot);
199c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien      } else {
200c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien        ExnSlot = FnToLPadSlotMap[F].first;
201c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien        SelSlot = FnToLPadSlotMap[F].second;
202c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien      }
203c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien
204c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien      if (!UnwindDest->getSinglePredecessor()) {
205c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien        // The unwind destination doesn't have a single predecessor. Create an
206c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien        // unwind destination which has only one predecessor.
207c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien        BasicBlock *NewBB = BasicBlock::Create(Context, "new.lpad",
208c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien                                               UnwindDest->getParent());
209c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien        BranchInst::Create(UnwindDest, NewBB);
210c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien        Invoke->setUnwindDest(NewBB);
211c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien
212c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien        // Fix up any PHIs in the original unwind destination block.
213c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien        for (BasicBlock::iterator
214c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien               II = UnwindDest->begin(); isa<PHINode>(II); ++II) {
215c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien          PHINode *PN = cast<PHINode>(II);
216c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien          int Idx = PN->getBasicBlockIndex(Invoke->getParent());
217c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien          if (Idx == -1) continue;
218c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien          PN->setIncomingBlock(Idx, NewBB);
219c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien        }
220c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien
221c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien        UnwindDest = NewBB;
222c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien      }
223c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien
224c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien      IRBuilder<> Builder(Context);
225c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien      Builder.SetInsertPoint(UnwindDest, UnwindDest->getFirstInsertionPt());
226c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien
227f229c40f0d2da19985e68955aef1a06ce4121e63Pirama Arumuga Nainar      LandingPadInst *LPI = Builder.CreateLandingPad(LPadSlotTy, 0);
228c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien      Value *LPExn = Builder.CreateExtractValue(LPI, 0);
229c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien      Value *LPSel = Builder.CreateExtractValue(LPI, 1);
230c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien      Builder.CreateStore(LPExn, ExnSlot);
231c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien      Builder.CreateStore(LPSel, SelSlot);
232c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien
233c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien      TransferClausesToLandingPadInst(LPI, Sel);
234c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien
235c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien      DeadInsts.insert(Exn);
236c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien      DeadInsts.insert(Sel);
237c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien    }
238c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien
239c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien    // Replace the old intrinsic calls with the values from the landingpad
240c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien    // instruction(s). These values were stored in allocas for us to use here.
241c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien    for (DenseMap<InvokeInst*, std::pair<Value*, Value*> >::iterator
242c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien           I = InvokeToIntrinsicsMap.begin(), E = InvokeToIntrinsicsMap.end();
243c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien         I != E; ++I) {
244c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien      std::pair<Value*, Value*> EHIntrinsics = I->second;
245c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien      CallInst *Exn = cast<CallInst>(EHIntrinsics.first);
246c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien      CallInst *Sel = cast<CallInst>(EHIntrinsics.second);
247c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien      BasicBlock *Parent = Exn->getParent();
248c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien
249c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien      std::pair<Value*,Value*> ExnSelSlots = FnToLPadSlotMap[Parent->getParent()];
250c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien
251c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien      IRBuilder<> Builder(Context);
252f229c40f0d2da19985e68955aef1a06ce4121e63Pirama Arumuga Nainar      Builder.SetInsertPoint(Parent, Exn->getIterator());
253c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien      LoadInst *LPExn = Builder.CreateLoad(ExnSelSlots.first, "exn.load");
254c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien      LoadInst *LPSel = Builder.CreateLoad(ExnSelSlots.second, "sel.load");
255c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien
256c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien      Exn->replaceAllUsesWith(LPExn);
257c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien      Sel->replaceAllUsesWith(LPSel);
258c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien    }
259c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien
260c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien    // Remove the dead instructions.
261c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien    for (SmallPtrSet<Instruction*, 32>::iterator
262c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien           I = DeadInsts.begin(), E = DeadInsts.end(); I != E; ++I) {
263c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien      Instruction *Inst = *I;
264c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien      Inst->eraseFromParent();
265c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien    }
266c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien
267c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien    // Replace calls to "llvm.eh.resume" with the 'resume' instruction. Load the
268c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien    // exception and selector values from the stored place.
269c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien    Function *EHResume = M->getFunction("llvm.eh.resume");
270c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien    if (!EHResume) return;
271c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien
272c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien    while (!EHResume->use_empty()) {
273c2074caf075818abb6d3689ad924ca09f4a5ba1fTim Murray      CallInst *Resume = cast<CallInst>(*EHResume->use_begin());
274c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien      BasicBlock *BB = Resume->getParent();
275c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien
276c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien      IRBuilder<> Builder(Context);
277f229c40f0d2da19985e68955aef1a06ce4121e63Pirama Arumuga Nainar      Builder.SetInsertPoint(BB, Resume->getIterator());
278c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien
279c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien      Value *LPadVal =
280c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien        Builder.CreateInsertValue(UndefValue::get(LPadSlotTy),
281c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien                                  Resume->getArgOperand(0), 0, "lpad.val");
282c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien      LPadVal = Builder.CreateInsertValue(LPadVal, Resume->getArgOperand(1),
283c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien                                          1, "lpad.val");
284c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien      Builder.CreateResume(LPadVal);
285c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien
286c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien      // Remove all instructions after the 'resume.'
287f229c40f0d2da19985e68955aef1a06ce4121e63Pirama Arumuga Nainar      BasicBlock::iterator I = Resume->getIterator();
288c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien      while (I != BB->end()) {
289c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien        Instruction *Inst = &*I++;
290c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien        Inst->eraseFromParent();
291c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien      }
292c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien    }
293c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien  }
294c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien
295c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien
296c2074caf075818abb6d3689ad924ca09f4a5ba1fTim Murray  void StripDebugInfoOfFunction(Module* M, const char* name) {
297c2074caf075818abb6d3689ad924ca09f4a5ba1fTim Murray    if (Function* FuncStart = M->getFunction(name)) {
298c2074caf075818abb6d3689ad924ca09f4a5ba1fTim Murray      while (!FuncStart->use_empty()) {
299c2074caf075818abb6d3689ad924ca09f4a5ba1fTim Murray        cast<CallInst>(*FuncStart->use_begin())->eraseFromParent();
300c2074caf075818abb6d3689ad924ca09f4a5ba1fTim Murray      }
301c2074caf075818abb6d3689ad924ca09f4a5ba1fTim Murray      FuncStart->eraseFromParent();
302c2074caf075818abb6d3689ad924ca09f4a5ba1fTim Murray    }
303c2074caf075818abb6d3689ad924ca09f4a5ba1fTim Murray  }
304c2074caf075818abb6d3689ad924ca09f4a5ba1fTim Murray
305c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien  /// This function strips all debug info intrinsics, except for llvm.dbg.declare.
306c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien  /// If an llvm.dbg.declare intrinsic is invalid, then this function simply
307c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien  /// strips that use.
308c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien  void CheckDebugInfoIntrinsics(Module *M) {
309c2074caf075818abb6d3689ad924ca09f4a5ba1fTim Murray    StripDebugInfoOfFunction(M, "llvm.dbg.func.start");
310c2074caf075818abb6d3689ad924ca09f4a5ba1fTim Murray    StripDebugInfoOfFunction(M, "llvm.dbg.stoppoint");
311c2074caf075818abb6d3689ad924ca09f4a5ba1fTim Murray    StripDebugInfoOfFunction(M, "llvm.dbg.region.start");
312c2074caf075818abb6d3689ad924ca09f4a5ba1fTim Murray    StripDebugInfoOfFunction(M, "llvm.dbg.region.end");
313c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien
314c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien    if (Function *Declare = M->getFunction("llvm.dbg.declare")) {
315c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien      if (!Declare->use_empty()) {
316c2074caf075818abb6d3689ad924ca09f4a5ba1fTim Murray        DbgDeclareInst *DDI = cast<DbgDeclareInst>(*Declare->use_begin());
3171bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        if (!isa<MDNode>(ValueAsMetadata::get(DDI->getArgOperand(0))) ||
3181bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines            !isa<MDNode>(ValueAsMetadata::get(DDI->getArgOperand(1)))) {
319c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien          while (!Declare->use_empty()) {
320c2074caf075818abb6d3689ad924ca09f4a5ba1fTim Murray            CallInst *CI = cast<CallInst>(*Declare->use_begin());
321c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien            CI->eraseFromParent();
322c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien          }
323c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien          Declare->eraseFromParent();
324c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien        }
325c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien      }
326c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien    }
327c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien  }
3285cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines
3295cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines
3305cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines//===----------------------------------------------------------------------===//
3315cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines//                          BitcodeReaderValueList Class
3325cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines//===----------------------------------------------------------------------===//
3335cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines
3345cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hinesclass BitcodeReaderValueList {
3355cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  std::vector<WeakVH> ValuePtrs;
3365cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines
3375cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  /// ResolveConstants - As we resolve forward-referenced constants, we add
3385cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  /// information about them to this vector.  This allows us to resolve them in
3395cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  /// bulk instead of resolving each reference at a time.  See the code in
3405cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  /// ResolveConstantForwardRefs for more information about this.
3415cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  ///
3425cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  /// The key of this vector is the placeholder constant, the value is the slot
3435cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  /// number that holds the resolved value.
3445cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  typedef std::vector<std::pair<Constant*, unsigned> > ResolveConstantsTy;
3455cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  ResolveConstantsTy ResolveConstants;
3465cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  LLVMContext &Context;
3475cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hinespublic:
3487e920a716693033edf32a6fedd03798bbfbd85ebChih-Hung Hsieh  explicit BitcodeReaderValueList(LLVMContext &C) : Context(C) {}
3495cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  ~BitcodeReaderValueList() {
3505cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines    assert(ResolveConstants.empty() && "Constants not resolved?");
3515cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  }
3525cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines
3535cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  // vector compatibility methods
3545cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  unsigned size() const { return ValuePtrs.size(); }
3555cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  void resize(unsigned N) { ValuePtrs.resize(N); }
3565cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  void push_back(Value *V) {
3575cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines    ValuePtrs.push_back(V);
3585cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  }
3595cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines
3605cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  void clear() {
3615cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines    assert(ResolveConstants.empty() && "Constants not resolved?");
3625cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines    ValuePtrs.clear();
3635cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  }
3645cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines
3655cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  Value *operator[](unsigned i) const {
3665cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines    assert(i < ValuePtrs.size());
3675cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines    return ValuePtrs[i];
3685cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  }
3695cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines
3705cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  Value *back() const { return ValuePtrs.back(); }
3715cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines    void pop_back() { ValuePtrs.pop_back(); }
3725cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  bool empty() const { return ValuePtrs.empty(); }
3735cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  void shrinkTo(unsigned N) {
3745cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines    assert(N <= size() && "Invalid shrinkTo request!");
3755cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines    ValuePtrs.resize(N);
3765cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  }
3775cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines
3785cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  Constant *getConstantFwdRef(unsigned Idx, Type *Ty);
3795cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  Value *getValueFwdRef(unsigned Idx, Type *Ty);
3805cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines
3815cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  void AssignValue(Value *V, unsigned Idx);
3825cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines
3835cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  /// ResolveConstantForwardRefs - Once all constants are read, this method bulk
3845cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  /// resolves any forward references.
3855cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  void ResolveConstantForwardRefs();
3865cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines};
3875cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines
3885cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines
3895cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines//===----------------------------------------------------------------------===//
3905cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines//                          BitcodeReaderMDValueList Class
3915cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines//===----------------------------------------------------------------------===//
3925cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines
3935cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hinesclass BitcodeReaderMDValueList {
3945cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  unsigned NumFwdRefs;
3955cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  bool AnyFwdRefs;
3965cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  std::vector<TrackingMDRef> MDValuePtrs;
3975cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines
3985cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  LLVMContext &Context;
3995cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hinespublic:
4007e920a716693033edf32a6fedd03798bbfbd85ebChih-Hung Hsieh  explicit BitcodeReaderMDValueList(LLVMContext &C)
4015cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines      : NumFwdRefs(0), AnyFwdRefs(false), Context(C) {}
4025cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines
4035cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  // vector compatibility methods
4045cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  unsigned size() const       { return MDValuePtrs.size(); }
4055cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  void resize(unsigned N)     { MDValuePtrs.resize(N); }
4065cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  void push_back(Metadata *MD) { MDValuePtrs.emplace_back(MD); }
4075cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  void clear()                { MDValuePtrs.clear();  }
4085cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  Metadata *back() const      { return MDValuePtrs.back(); }
4095cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  void pop_back()             { MDValuePtrs.pop_back(); }
4105cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  bool empty() const          { return MDValuePtrs.empty(); }
4115cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines
4125cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  Metadata *operator[](unsigned i) const {
4135cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines    assert(i < MDValuePtrs.size());
4145cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines    return MDValuePtrs[i];
4155cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  }
4165cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines
4175cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  void shrinkTo(unsigned N) {
4185cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines    assert(N <= size() && "Invalid shrinkTo request!");
4195cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines    MDValuePtrs.resize(N);
4205cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  }
4215cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines
4225cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  Metadata *getValueFwdRef(unsigned Idx);
4235cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  void AssignValue(Metadata *MD, unsigned Idx);
4245cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  void tryToResolveCycles();
4255cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines};
4265cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines
4275cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hinesclass BitcodeReader : public GVMaterializer {
4285cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  LLVMContext &Context;
4295cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  DiagnosticHandlerFunction DiagnosticHandler;
4305cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  Module *TheModule;
4315cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  std::unique_ptr<MemoryBuffer> Buffer;
4325cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  std::unique_ptr<BitstreamReader> StreamFile;
4335cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  BitstreamCursor Stream;
434f229c40f0d2da19985e68955aef1a06ce4121e63Pirama Arumuga Nainar  std::unique_ptr<DataStreamer> LazyStreamer;
4355cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  uint64_t NextUnreadBit;
4365cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  bool SeenValueSymbolTable;
4375cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines
4385cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  std::vector<Type*> TypeList;
4395cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  BitcodeReaderValueList ValueList;
4405cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  BitcodeReaderMDValueList MDValueList;
4415cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  SmallVector<Instruction *, 64> InstructionList;
4425cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines
4435cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInits;
4445cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  std::vector<std::pair<GlobalAlias*, unsigned> > AliasInits;
4455cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines
4465cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  /// MAttributes - The set of attributes by index.  Index zero in the
4475cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  /// file is for null, and is thus not represented here.  As such all indices
4485cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  /// are off by one.
4495cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  std::vector<AttributeSet> MAttributes;
4505cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines
4515cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  /// \brief The set of attribute groups.
4525cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  std::map<unsigned, AttributeSet> MAttributeGroups;
4535cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines
4545cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  /// FunctionBBs - While parsing a function body, this is a list of the basic
4555cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  /// blocks for the function.
4565cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  std::vector<BasicBlock*> FunctionBBs;
4575cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines
4585cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  // When reading the module header, this list is populated with functions that
4595cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  // have bodies later in the file.
4605cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  std::vector<Function*> FunctionsWithBodies;
4615cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines
4625cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  // When intrinsic functions are encountered which require upgrading they are
4635cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  // stored here with their replacement function.
4645cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  typedef std::vector<std::pair<Function*, Function*> > UpgradedIntrinsicMap;
4655cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  UpgradedIntrinsicMap UpgradedIntrinsics;
4665cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines
4675cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  // Map the bitcode's custom MDKind ID to the Module's MDKind ID.
4685cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  DenseMap<unsigned, unsigned> MDKindMap;
4695cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines
4705cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  // Several operations happen after the module header has been read, but
4715cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  // before function bodies are processed. This keeps track of whether
4725cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  // we've done this yet.
4735cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  bool SeenFirstFunctionBody;
4745cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines
4755cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  /// DeferredFunctionInfo - When function bodies are initially scanned, this
4765cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  /// map contains info about where to find deferred function body in the
4775cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  /// stream.
4785cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  DenseMap<Function*, uint64_t> DeferredFunctionInfo;
4795cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines
4805cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  /// BlockAddrFwdRefs - These are blockaddr references to basic blocks.  These
4815cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  /// are resolved lazily when functions are loaded.
4825cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  typedef std::pair<unsigned, GlobalVariable*> BlockAddrRefTy;
4835cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  DenseMap<Function*, std::vector<BlockAddrRefTy> > BlockAddrFwdRefs;
4845cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines
4855cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  static const std::error_category &BitcodeErrorCategory();
4865cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines
4875cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hinespublic:
4885cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  std::error_code Error(BitcodeError E, const Twine &Message);
4895cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  std::error_code Error(BitcodeError E);
4905cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  std::error_code Error(const Twine &Message);
4915cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines
4925cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  explicit BitcodeReader(MemoryBuffer *buffer, LLVMContext &C,
4935cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines                         DiagnosticHandlerFunction DiagnosticHandler);
4945cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  ~BitcodeReader() { FreeState(); }
4955cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines
4965cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  void FreeState();
4975cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines
4985cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  void releaseBuffer();
4995cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines
500f229c40f0d2da19985e68955aef1a06ce4121e63Pirama Arumuga Nainar  bool isDematerializable(const GlobalValue *GV) const;
5015cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  std::error_code materialize(GlobalValue *GV) override;
502f229c40f0d2da19985e68955aef1a06ce4121e63Pirama Arumuga Nainar  std::error_code materializeModule() override;
5035cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  std::vector<StructType *> getIdentifiedStructTypes() const override;
504f229c40f0d2da19985e68955aef1a06ce4121e63Pirama Arumuga Nainar  void dematerialize(GlobalValue *GV);
5055cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines
5065cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  /// @brief Main interface to parsing a bitcode buffer.
5075cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  /// @returns true if an error occurred.
5085cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  std::error_code ParseBitcodeInto(Module *M);
5095cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines
5105cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  /// @brief Cheap mechanism to just extract module triple
5115cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  /// @returns true if an error occurred.
5125cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  llvm::ErrorOr<std::string> parseTriple();
5135cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines
5145cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  static uint64_t decodeSignRotatedValue(uint64_t V);
5155cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines
5165cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  /// Materialize any deferred Metadata block.
5175cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  std::error_code materializeMetadata() override;
5185cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines
51998137cca7eebca946b869b010fef2821c9bf4971Pirama Arumuga Nainar  void setStripDebugInfo() override;
52098137cca7eebca946b869b010fef2821c9bf4971Pirama Arumuga Nainar
5215cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hinesprivate:
5225cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  std::vector<StructType *> IdentifiedStructTypes;
5235cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  StructType *createIdentifiedStructType(LLVMContext &Context, StringRef Name);
5245cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  StructType *createIdentifiedStructType(LLVMContext &Context);
5255cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines
5265cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  Type *getTypeByID(unsigned ID);
5275cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  Type *getTypeByIDOrNull(unsigned ID);
5285cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  Value *getFnValueByID(unsigned ID, Type *Ty) {
5295cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines    if (Ty && Ty->isMetadataTy())
5305cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines      return MetadataAsValue::get(Ty->getContext(), getFnMetadataByID(ID));
5315cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines    return ValueList.getValueFwdRef(ID, Ty);
5325cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  }
5335cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  Metadata *getFnMetadataByID(unsigned ID) {
5345cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines    return MDValueList.getValueFwdRef(ID);
5355cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  }
5365cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  BasicBlock *getBasicBlock(unsigned ID) const {
5375cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines    if (ID >= FunctionBBs.size()) return nullptr; // Invalid ID
5385cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines    return FunctionBBs[ID];
5395cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  }
5405cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  AttributeSet getAttributes(unsigned i) const {
5415cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines    if (i-1 < MAttributes.size())
5425cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines      return MAttributes[i-1];
5435cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines    return AttributeSet();
5445cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  }
5455cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines
5465cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  /// getValueTypePair - Read a value/type pair out of the specified record from
5475cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  /// slot 'Slot'.  Increment Slot past the number of slots used in the record.
5485cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  /// Return true on failure.
5495cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  bool getValueTypePair(SmallVectorImpl<uint64_t> &Record, unsigned &Slot,
5505cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines                        unsigned InstNum, Value *&ResVal) {
5515cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines    if (Slot == Record.size()) return true;
5525cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines    unsigned ValNo = (unsigned)Record[Slot++];
5535cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines    if (ValNo < InstNum) {
5545cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines      // If this is not a forward reference, just return the value we already
5555cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines      // have.
5565cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines      ResVal = getFnValueByID(ValNo, nullptr);
5575cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines      return ResVal == nullptr;
5585cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines    } else if (Slot == Record.size()) {
5595cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines      return true;
5605cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines    }
5615cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines
5625cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines    unsigned TypeNo = (unsigned)Record[Slot++];
5635cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines    ResVal = getFnValueByID(ValNo, getTypeByID(TypeNo));
5645cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines    return ResVal == nullptr;
5655cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  }
5665cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  bool getValue(SmallVector<uint64_t, 64> &Record, unsigned &Slot,
5675cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines                Type *Ty, Value *&ResVal) {
5685cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines    if (Slot == Record.size()) return true;
5695cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines    unsigned ValNo = (unsigned)Record[Slot++];
5705cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines    ResVal = getFnValueByID(ValNo, Ty);
5715cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines    return ResVal == 0;
5725cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  }
5735cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines
5745cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines
5755cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  std::error_code ParseModule(bool Resume);
5765cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  std::error_code ParseAttributeBlock();
5775cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  std::error_code ParseTypeTable();
5785cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  std::error_code ParseOldTypeTable();         // FIXME: Remove in LLVM 3.1
5795cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  std::error_code ParseTypeTableBody();
5805cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines
5815cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  std::error_code ParseOldTypeSymbolTable();   // FIXME: Remove in LLVM 3.1
5825cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  std::error_code ParseValueSymbolTable();
5835cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  std::error_code ParseConstants();
5845cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  std::error_code RememberAndSkipFunctionBody();
5855cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  std::error_code ParseFunctionBody(Function *F);
5865cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  std::error_code GlobalCleanup();
5875cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  std::error_code ResolveGlobalAndAliasInits();
5885cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  std::error_code ParseMetadata();
5895cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  std::error_code ParseMetadataAttachment();
5905cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  llvm::ErrorOr<std::string> parseModuleTriple();
5915cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  std::error_code InitStream();
5925cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  std::error_code InitStreamFromBuffer();
5935cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  std::error_code InitLazyStream();
5945cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines};
5955cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines
596c7d67a701663191bcdab2416c11b69fae16d49fbLogan Chien} // end anonymous namespace
5973bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien
5988a019dd0040bedf5078e4d18e06a244a675b80e8Chih-Hung Hsiehstatic std::error_code Error(const DiagnosticHandlerFunction &DiagnosticHandler,
5991bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines                             std::error_code EC, const Twine &Message) {
6001bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  BitcodeDiagnosticInfo DI(EC, DS_Error, Message);
6011bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  DiagnosticHandler(DI);
6021bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  return EC;
6031bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines}
6041bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines
6058a019dd0040bedf5078e4d18e06a244a675b80e8Chih-Hung Hsiehstatic std::error_code Error(const DiagnosticHandlerFunction &DiagnosticHandler,
6061bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines                             std::error_code EC) {
6071bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  return Error(DiagnosticHandler, EC, EC.message());
6081bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines}
6091bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines
6101bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hinesstd::error_code BitcodeReader::Error(BitcodeError E, const Twine &Message) {
6111bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  return ::Error(DiagnosticHandler, make_error_code(E), Message);
6121bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines}
6131bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines
6141bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hinesstd::error_code BitcodeReader::Error(const Twine &Message) {
6151bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  return ::Error(DiagnosticHandler,
6161bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines                 make_error_code(BitcodeError::CorruptedBitcode), Message);
6171bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines}
6181bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines
6191bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hinesstd::error_code BitcodeReader::Error(BitcodeError E) {
6201bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  return ::Error(DiagnosticHandler, make_error_code(E));
6211bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines}
6221bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines
6231bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hinesstatic DiagnosticHandlerFunction getDiagHandler(DiagnosticHandlerFunction F,
6241bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines                                                LLVMContext &C) {
6251bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  if (F)
6261bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines    return F;
6271bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  return [&C](const DiagnosticInfo &DI) { C.diagnose(DI); };
6281bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines}
6291bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines
6301bd9f627fa0affb457507e86b0b6684c695fe726Stephen HinesBitcodeReader::BitcodeReader(MemoryBuffer *buffer, LLVMContext &C,
6311bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines                             DiagnosticHandlerFunction DiagnosticHandler)
6321bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines    : Context(C), DiagnosticHandler(getDiagHandler(DiagnosticHandler, C)),
6331bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines      TheModule(nullptr), Buffer(buffer), LazyStreamer(nullptr),
6341bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines      NextUnreadBit(0), SeenValueSymbolTable(false), ValueList(C),
6351bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines      MDValueList(C), SeenFirstFunctionBody(false) {}
6361bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines
6371bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines
6383bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chienvoid BitcodeReader::FreeState() {
639579361346abc6696c805e3904a18178ebce4e4a3Stephen Hines  Buffer = nullptr;
6403bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien  std::vector<Type*>().swap(TypeList);
6413bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien  ValueList.clear();
6423bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien  MDValueList.clear();
6433bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien
644b730e239619a546d93e5926ea92d698ab77ec7f6Stephen Hines  std::vector<AttributeSet>().swap(MAttributes);
6453bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien  std::vector<BasicBlock*>().swap(FunctionBBs);
6463bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien  std::vector<Function*>().swap(FunctionsWithBodies);
6473bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien  DeferredFunctionInfo.clear();
6483bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien  MDKindMap.clear();
6493bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien}
6503bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien
6513bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien//===----------------------------------------------------------------------===//
6523bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien//  Helper functions to implement forward reference resolution, etc.
6533bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien//===----------------------------------------------------------------------===//
6543bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien
6553bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien/// ConvertToString - Convert a string from a record into an std::string, return
6563bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien/// true on failure.
6573bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chientemplate<typename StrTy>
6581bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hinesstatic bool ConvertToString(ArrayRef<uint64_t> Record, unsigned Idx,
6593bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien                            StrTy &Result) {
6603bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien  if (Idx > Record.size())
6613bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien    return true;
6623bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien
6633bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien  for (unsigned i = Idx, e = Record.size(); i != e; ++i)
6643bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien    Result += (char)Record[i];
6653bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien  return false;
6663bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien}
6673bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien
6681bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hinesstatic GlobalValue::LinkageTypes getDecodedLinkage(unsigned Val) {
6693bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien  switch (Val) {
6703bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien  default: // Map unknown/new linkages to external
6711bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  case 0:
6721bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines    return GlobalValue::ExternalLinkage;
6731bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  case 1:
6741bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines    return GlobalValue::WeakAnyLinkage;
6751bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  case 2:
6761bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines    return GlobalValue::AppendingLinkage;
6771bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  case 3:
6781bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines    return GlobalValue::InternalLinkage;
6791bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  case 4:
6801bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines    return GlobalValue::LinkOnceAnyLinkage;
6811bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  case 5:
6821bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines    return GlobalValue::ExternalLinkage; // Obsolete DLLImportLinkage
6831bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  case 6:
6841bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines    return GlobalValue::ExternalLinkage; // Obsolete DLLExportLinkage
6851bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  case 7:
6861bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines    return GlobalValue::ExternalWeakLinkage;
6871bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  case 8:
6881bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines    return GlobalValue::CommonLinkage;
6891bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  case 9:
6901bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines    return GlobalValue::PrivateLinkage;
6911bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  case 10:
6921bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines    return GlobalValue::WeakODRLinkage;
6931bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  case 11:
6941bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines    return GlobalValue::LinkOnceODRLinkage;
6951bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  case 12:
6961bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines    return GlobalValue::AvailableExternallyLinkage;
6971bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  case 13:
6981bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines    return GlobalValue::PrivateLinkage; // Obsolete LinkerPrivateLinkage
6991bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  case 14:
7001bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines    return GlobalValue::ExternalWeakLinkage; // Obsolete LinkerPrivateWeakLinkage
701d724d097437f40a5689464429f948ec41e4a2415Stephen Hines  //ANDROID: convert LinkOnceODRAutoHideLinkage -> LinkOnceODRLinkage
7021bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  case 15:
7031bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines    return GlobalValue::LinkOnceODRLinkage;
7043bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien  }
7053bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien}
7063bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien
7073bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chienstatic GlobalValue::VisibilityTypes GetDecodedVisibility(unsigned Val) {
7083bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien  switch (Val) {
7093bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien  default: // Map unknown visibilities to default.
7103bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien  case 0: return GlobalValue::DefaultVisibility;
7113bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien  case 1: return GlobalValue::HiddenVisibility;
7123bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien  case 2: return GlobalValue::ProtectedVisibility;
7133bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien  }
7143bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien}
7153bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien
7168b5be8604307d0dd342235803dd45775830b84c6Shih-wei Liaostatic GlobalVariable::ThreadLocalMode GetDecodedThreadLocalMode(unsigned Val) {
7178b5be8604307d0dd342235803dd45775830b84c6Shih-wei Liao  switch (Val) {
7188b5be8604307d0dd342235803dd45775830b84c6Shih-wei Liao    case 0: return GlobalVariable::NotThreadLocal;
7198b5be8604307d0dd342235803dd45775830b84c6Shih-wei Liao    default: // Map unknown non-zero value to general dynamic.
7208b5be8604307d0dd342235803dd45775830b84c6Shih-wei Liao    case 1: return GlobalVariable::GeneralDynamicTLSModel;
7218b5be8604307d0dd342235803dd45775830b84c6Shih-wei Liao    case 2: return GlobalVariable::LocalDynamicTLSModel;
7228b5be8604307d0dd342235803dd45775830b84c6Shih-wei Liao    case 3: return GlobalVariable::InitialExecTLSModel;
7238b5be8604307d0dd342235803dd45775830b84c6Shih-wei Liao    case 4: return GlobalVariable::LocalExecTLSModel;
7248b5be8604307d0dd342235803dd45775830b84c6Shih-wei Liao  }
7258b5be8604307d0dd342235803dd45775830b84c6Shih-wei Liao}
7268b5be8604307d0dd342235803dd45775830b84c6Shih-wei Liao
727f5b49a0ca149b09c8306b86db9f3aca703c4acd5Pirama Arumuga Nainarstatic GlobalVariable::UnnamedAddr getDecodedUnnamedAddrType(unsigned Val) {
728f5b49a0ca149b09c8306b86db9f3aca703c4acd5Pirama Arumuga Nainar  switch (Val) {
729f5b49a0ca149b09c8306b86db9f3aca703c4acd5Pirama Arumuga Nainar    default: // Map unknown to UnnamedAddr::None.
730f5b49a0ca149b09c8306b86db9f3aca703c4acd5Pirama Arumuga Nainar    case 0: return GlobalVariable::UnnamedAddr::None;
731f5b49a0ca149b09c8306b86db9f3aca703c4acd5Pirama Arumuga Nainar    case 1: return GlobalVariable::UnnamedAddr::Global;
732f5b49a0ca149b09c8306b86db9f3aca703c4acd5Pirama Arumuga Nainar    case 2: return GlobalVariable::UnnamedAddr::Local;
733f5b49a0ca149b09c8306b86db9f3aca703c4acd5Pirama Arumuga Nainar  }
734f5b49a0ca149b09c8306b86db9f3aca703c4acd5Pirama Arumuga Nainar}
735f5b49a0ca149b09c8306b86db9f3aca703c4acd5Pirama Arumuga Nainar
7363bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chienstatic int GetDecodedCastOpcode(unsigned Val) {
7373bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien  switch (Val) {
7383bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien  default: return -1;
7393bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien  case bitc::CAST_TRUNC   : return Instruction::Trunc;
7403bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien  case bitc::CAST_ZEXT    : return Instruction::ZExt;
7413bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien  case bitc::CAST_SEXT    : return Instruction::SExt;
7423bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien  case bitc::CAST_FPTOUI  : return Instruction::FPToUI;
7433bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien  case bitc::CAST_FPTOSI  : return Instruction::FPToSI;
7443bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien  case bitc::CAST_UITOFP  : return Instruction::UIToFP;
7453bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien  case bitc::CAST_SITOFP  : return Instruction::SIToFP;
7463bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien  case bitc::CAST_FPTRUNC : return Instruction::FPTrunc;
7473bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien  case bitc::CAST_FPEXT   : return Instruction::FPExt;
7483bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien  case bitc::CAST_PTRTOINT: return Instruction::PtrToInt;
7493bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien  case bitc::CAST_INTTOPTR: return Instruction::IntToPtr;
7503bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien  case bitc::CAST_BITCAST : return Instruction::BitCast;
7513bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien  }
7523bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien}
7533bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chienstatic int GetDecodedBinaryOpcode(unsigned Val, Type *Ty) {
7543bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien  switch (Val) {
7553bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien  default: return -1;
7563bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien  case bitc::BINOP_ADD:
7573bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien    return Ty->isFPOrFPVectorTy() ? Instruction::FAdd : Instruction::Add;
7583bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien  case bitc::BINOP_SUB:
7593bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien    return Ty->isFPOrFPVectorTy() ? Instruction::FSub : Instruction::Sub;
7603bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien  case bitc::BINOP_MUL:
7613bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien    return Ty->isFPOrFPVectorTy() ? Instruction::FMul : Instruction::Mul;
7623bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien  case bitc::BINOP_UDIV: return Instruction::UDiv;
7633bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien  case bitc::BINOP_SDIV:
7643bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien    return Ty->isFPOrFPVectorTy() ? Instruction::FDiv : Instruction::SDiv;
7653bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien  case bitc::BINOP_UREM: return Instruction::URem;
7663bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien  case bitc::BINOP_SREM:
7673bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien    return Ty->isFPOrFPVectorTy() ? Instruction::FRem : Instruction::SRem;
7683bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien  case bitc::BINOP_SHL:  return Instruction::Shl;
7693bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien  case bitc::BINOP_LSHR: return Instruction::LShr;
7703bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien  case bitc::BINOP_ASHR: return Instruction::AShr;
7713bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien  case bitc::BINOP_AND:  return Instruction::And;
7723bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien  case bitc::BINOP_OR:   return Instruction::Or;
7733bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien  case bitc::BINOP_XOR:  return Instruction::Xor;
7743bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien  }
7753bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien}
7763bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien
7773bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chienstatic AtomicRMWInst::BinOp GetDecodedRMWOperation(unsigned Val) {
7783bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien  switch (Val) {
7793bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien  default: return AtomicRMWInst::BAD_BINOP;
7803bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien  case bitc::RMW_XCHG: return AtomicRMWInst::Xchg;
7813bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien  case bitc::RMW_ADD: return AtomicRMWInst::Add;
7823bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien  case bitc::RMW_SUB: return AtomicRMWInst::Sub;
7833bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien  case bitc::RMW_AND: return AtomicRMWInst::And;
7843bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien  case bitc::RMW_NAND: return AtomicRMWInst::Nand;
7853bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien  case bitc::RMW_OR: return AtomicRMWInst::Or;
7863bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien  case bitc::RMW_XOR: return AtomicRMWInst::Xor;
7873bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien  case bitc::RMW_MAX: return AtomicRMWInst::Max;
7883bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien  case bitc::RMW_MIN: return AtomicRMWInst::Min;
7893bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien  case bitc::RMW_UMAX: return AtomicRMWInst::UMax;
7903bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien  case bitc::RMW_UMIN: return AtomicRMWInst::UMin;
7913bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien  }
7923bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien}
7933bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien
7943bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chienstatic AtomicOrdering GetDecodedOrdering(unsigned Val) {
7953bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien  switch (Val) {
796f5b49a0ca149b09c8306b86db9f3aca703c4acd5Pirama Arumuga Nainar  case bitc::ORDERING_NOTATOMIC: return AtomicOrdering::NotAtomic;
797f5b49a0ca149b09c8306b86db9f3aca703c4acd5Pirama Arumuga Nainar  case bitc::ORDERING_UNORDERED: return AtomicOrdering::Unordered;
798f5b49a0ca149b09c8306b86db9f3aca703c4acd5Pirama Arumuga Nainar  case bitc::ORDERING_MONOTONIC: return AtomicOrdering::Monotonic;
799f5b49a0ca149b09c8306b86db9f3aca703c4acd5Pirama Arumuga Nainar  case bitc::ORDERING_ACQUIRE: return AtomicOrdering::Acquire;
800f5b49a0ca149b09c8306b86db9f3aca703c4acd5Pirama Arumuga Nainar  case bitc::ORDERING_RELEASE: return AtomicOrdering::Release;
801f5b49a0ca149b09c8306b86db9f3aca703c4acd5Pirama Arumuga Nainar  case bitc::ORDERING_ACQREL: return AtomicOrdering::AcquireRelease;
8023bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien  default: // Map unknown orderings to sequentially-consistent.
803f5b49a0ca149b09c8306b86db9f3aca703c4acd5Pirama Arumuga Nainar  case bitc::ORDERING_SEQCST: return AtomicOrdering::SequentiallyConsistent;
8043bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien  }
8053bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien}
8063bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien
8073bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chienstatic SynchronizationScope GetDecodedSynchScope(unsigned Val) {
8083bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien  switch (Val) {
8093bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien  case bitc::SYNCHSCOPE_SINGLETHREAD: return SingleThread;
8103bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien  default: // Map unknown scopes to cross-thread.
8113bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien  case bitc::SYNCHSCOPE_CROSSTHREAD: return CrossThread;
8123bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien  }
8133bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien}
8143bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien
8153bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chiennamespace llvm {
8163bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chiennamespace {
8173bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien  /// @brief A class for maintaining the slot number definition
8183bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien  /// as a placeholder for the actual definition for forward constants defs.
8193bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien  class ConstantPlaceHolder : public ConstantExpr {
8201bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines    void operator=(const ConstantPlaceHolder &) = delete;
8213bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien  public:
8223bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien    // allocate space for exactly one operand
8233bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien    void *operator new(size_t s) {
8243bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien      return User::operator new(s, 1);
8253bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien    }
8263bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien    explicit ConstantPlaceHolder(Type *Ty, LLVMContext& Context)
8273bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien      : ConstantExpr(Ty, Instruction::UserOp1, &Op<0>(), 1) {
8283bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien      Op<0>() = UndefValue::get(Type::getInt32Ty(Context));
8293bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien    }
8303bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien
8313bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien    /// @brief Methods to support type inquiry through isa, cast, and dyn_cast.
8323bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien    static bool classof(const Value *V) {
8333bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien      return isa<ConstantExpr>(V) &&
8343bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien             cast<ConstantExpr>(V)->getOpcode() == Instruction::UserOp1;
8353bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien    }
8363bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien
8373bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien
8383bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien    /// Provide fast operand accessors
8391bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines    DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
8403bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien  };
8413bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien}
8423bdc2a8fde18e9e82a1e6ccef1c7379ef57bca35Logan Chien