llvm.org GIT mirror llvm / 50b136d
add bitcode reader support for blockaddress. We can now fully round trip blockaddress through .ll and .bc files, so add a testcase. There are still a bunch of places in the optimizer and other places that need to be updated to work with these constructs, but at least the basics are in now. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@85377 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 10 years ago
3 changed file(s) with 52 addition(s) and 3 deletion(s). Raw diff Collapse all Expand all
11911191 AsmStr, ConstrStr, HasSideEffects, IsAlignStack);
11921192 break;
11931193 }
1194 case bitc::CST_CODE_BLOCKADDRESS:{
1195 if (Record.size() < 3) return Error("Invalid CE_BLOCKADDRESS record");
1196 const Type *FnTy = getTypeByID(Record[0]);
1197 if (FnTy == 0) return Error("Invalid CE_BLOCKADDRESS record");
1198 Function *Fn =
1199 dyn_cast_or_null(ValueList.getConstantFwdRef(Record[1],FnTy));
1200 if (Fn == 0) return Error("Invalid CE_BLOCKADDRESS record");
1201
1202 GlobalVariable *FwdRef = new GlobalVariable(*Fn->getParent(),
1203 Type::getInt8Ty(Context),
1204 false, GlobalValue::InternalLinkage,
1205 0, "");
1206 BlockAddrFwdRefs[Fn].push_back(std::make_pair(Record[2], FwdRef));
1207 V = FwdRef;
1208 break;
1209 }
11941210 }
11951211
11961212 ValueList.AssignValue(V, NextCstNo);
22472263 }
22482264 }
22492265
2266 // See if anything took the address of blocks in this function. If so,
2267 // resolve them now.
2268 /// BlockAddrFwdRefs - These are blockaddr references to basic blocks. These
2269 /// are resolved lazily when functions are loaded.
2270 DenseMap >::iterator BAFRI =
2271 BlockAddrFwdRefs.find(F);
2272 if (BAFRI != BlockAddrFwdRefs.end()) {
2273 std::vector &RefList = BAFRI->second;
2274 for (unsigned i = 0, e = RefList.size(); i != e; ++i) {
2275 unsigned BlockIdx = RefList[i].first;
2276 if (BlockIdx >= FunctionBBs.size())
2277 return Error("Invalid blockaddress block #");
2278
2279 GlobalVariable *FwdRef = RefList[i].second;
2280 FwdRef->replaceAllUsesWith(BlockAddress::get(F, FunctionBBs[BlockIdx]));
2281 FwdRef->eraseFromParent();
2282 }
2283
2284 BlockAddrFwdRefs.erase(BAFRI);
2285 }
2286
22502287 // Trim the value list down to the size it was before we parsed this function.
22512288 ValueList.shrinkTo(ModuleValueListSize);
22522289 std::vector().swap(FunctionBBs);
9393 class BitcodeReaderMDValueList {
9494 std::vector MDValuePtrs;
9595
96 LLVMContext& Context;
96 LLVMContext &Context;
9797 public:
9898 BitcodeReaderMDValueList(LLVMContext& C) : Context(C) {}
9999
121121 };
122122
123123 class BitcodeReader : public ModuleProvider {
124 LLVMContext& Context;
124 LLVMContext &Context;
125125 MemoryBuffer *Buffer;
126126 BitstreamReader StreamFile;
127127 BitstreamCursor Stream;
162162 /// map contains info about where to find deferred function body (in the
163163 /// stream) and what linkage the original function had.
164164 DenseMap > DeferredFunctionInfo;
165
166 /// BlockAddrFwdRefs - These are blockaddr references to basic blocks. These
167 /// are resolved lazily when functions are loaded.
168 typedef std::pair BlockAddrRefTy;
169 DenseMap > BlockAddrFwdRefs;
170
165171 public:
166172 explicit BitcodeReader(MemoryBuffer *buffer, LLVMContext& C)
167173 : Context(C), Buffer(buffer), ErrorString(0), ValueList(C), MDValueList(C) {
2424 }
2525
2626
27 @Addr = global i8* blockaddress(@indbrtest, %BB1)
28 @Addr3 = global i8* blockaddress(@squared, %Case1)
29
2730
2831 define i32 @indbrtest(i8* %P, i32* %Q) {
2932 indirectbr i8* %P, [label %BB1, label %BB2, label %BB3]
3033 BB1:
3134 indirectbr i32* %Q, []
3235 BB2:
33 indirectbr i32* %Q, [label %BB1, label %BB2]
36 %R = bitcast i8* blockaddress(@indbrtest, %BB3) to i8*
37 indirectbr i8* %R, [label %BB1, label %BB2, label %BB3]
3438 BB3:
3539 ret i32 2
3640 }
41
42