llvm.org GIT mirror llvm / a7c49aa
handle function-level forward references, read binops. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@36620 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 13 years ago
2 changed file(s) with 149 addition(s) and 24 deletion(s). Raw diff Collapse all Expand all
1414 #include "BitcodeReader.h"
1515 #include "llvm/Constants.h"
1616 #include "llvm/DerivedTypes.h"
17 #include "llvm/Instructions.h"
1718 #include "llvm/Module.h"
1819 #include "llvm/ADT/SmallString.h"
1920 #include "llvm/Support/MathExtras.h"
124125 NumOperands = Idx+1;
125126 }
126127
127 if (Uses[Idx]) {
128 assert(Ty == getOperand(Idx)->getType() &&
129 "Type mismatch in constant table!");
130 return cast(getOperand(Idx));
128 if (Value *V = Uses[Idx]) {
129 assert(Ty == V->getType() && "Type mismatch in constant table!");
130 return cast(V);
131131 }
132132
133133 // Create and return a placeholder, which will later be RAUW'd.
134134 Constant *C = new ConstantPlaceHolder(Ty);
135135 Uses[Idx].init(C, this);
136136 return C;
137 }
138
139 Value *BitcodeReaderValueList::getValueFwdRef(unsigned Idx, const Type *Ty) {
140 if (Idx >= size()) {
141 // Insert a bunch of null values.
142 Uses.resize(Idx+1);
143 OperandList = &Uses[0];
144 NumOperands = Idx+1;
145 }
146
147 if (Value *V = Uses[Idx]) {
148 assert((Ty == 0 || Ty == V->getType()) && "Type mismatch in value table!");
149 return V;
150 }
151
152 // Create and return a placeholder, which will later be RAUW'd.
153 Value *V = new Argument(Ty);
154 Uses[Idx].init(V, this);
155 return V;
137156 }
138157
139158
149168 TypeList.push_back(OpaqueType::get());
150169 return TypeList.back().get();
151170 }
152
153171
154172 bool BitcodeReader::ParseTypeTable() {
155173 if (Stream.EnterSubBlock())
642660 }
643661 }
644662
645 if (NextCstNo == ValueList.size())
646 ValueList.push_back(V);
647 else if (ValueList[NextCstNo] == 0)
648 ValueList.initVal(NextCstNo, V);
649 else {
650 // If there was a forward reference to this constant,
651 Value *OldV = ValueList[NextCstNo];
652 ValueList.setOperand(NextCstNo, V);
653 OldV->replaceAllUsesWith(V);
654 delete OldV;
655 }
656
663 ValueList.AssignValue(V, NextCstNo);
657664 ++NextCstNo;
658665 }
659666 }
9971004 for(Function::arg_iterator I = F->arg_begin(), E = F->arg_end(); I != E; ++I)
9981005 ValueList.push_back(I);
9991006
1007 unsigned NextValueNo = ValueList.size();
1008
10001009 // Read all the records.
10011010 SmallVector Record;
10021011 while (1) {
10151024 break;
10161025 case bitc::CONSTANTS_BLOCK_ID:
10171026 if (ParseConstants()) return true;
1027 NextValueNo = ValueList.size();
10181028 break;
10191029 case bitc::VALUE_SYMTAB_BLOCK_ID:
10201030 if (ParseValueSymbolTable()) return true;
10301040
10311041 // Read a record.
10321042 Record.clear();
1043 Instruction *I = 0;
1044 BasicBlock *CurBB = 0;
1045 unsigned CurBBNo = 0;
10331046 switch (Stream.ReadRecord(Code, Record)) {
1034 default: // Default behavior: unknown constant
1047 default: // Default behavior: reject
1048 return Error("Unknown instruction");
10351049 case bitc::FUNC_CODE_DECLAREBLOCKS: // DECLAREBLOCKS: [nblocks]
1036 if (Record.size() < 1)
1037 return Error("Invalid FUNC_CODE_DECLAREBLOCKS record");
1050 if (Record.size() < 1 || Record[0] == 0)
1051 return Error("Invalid DECLAREBLOCKS record");
10381052 // Create all the basic blocks for the function.
10391053 FunctionBBs.resize(Record.size());
10401054 for (unsigned i = 0, e = FunctionBBs.size(); i != e; ++i)
10411055 FunctionBBs[i] = new BasicBlock("", F);
1042 break;
1043 }
1044 }
1045
1056 CurBB = FunctionBBs[0];
1057 continue;
1058
1059 case bitc::FUNC_CODE_INST_BINOP: {
1060 // BINOP: [opcode, ty, opval, opval]
1061 if (Record.size() < 4) return Error("Invalid BINOP record");
1062 const Type *Ty = getTypeByID(Record[1]);
1063 int Opc = GetDecodedBinaryOpcode(Record[0], Ty);
1064 Value *LHS = getFnValueByID(Record[2], Ty);
1065 Value *RHS = getFnValueByID(Record[3], Ty);
1066 if (Opc == -1 || Ty == 0 || LHS == 0 || RHS == 0)
1067 return Error("Invalid BINOP record");
1068 I = BinaryOperator::create((Instruction::BinaryOps)Opc, LHS, RHS);
1069 break;
1070 }
1071 #if 0
1072 case bitc::FUNC_CODE_INST_CAST:
1073 // CAST: [opcode, ty, opty, opval]
1074 case bitc::FUNC_CODE_INST_GEP:
1075 // GEP: [n, n x operands]
1076 case bitc::FUNC_CODE_INST_SELECT:
1077 // SELECT: [ty, opval, opval, opval]
1078 case bitc::FUNC_CODE_INST_EXTRACTELT:
1079 // EXTRACTELT: [opty, opval, opval]
1080 case bitc::FUNC_CODE_INST_INSERTELT:
1081 // INSERTELT: [ty, opval, opval, opval]
1082 case bitc::FUNC_CODE_INST_SHUFFLEVEC:
1083 // SHUFFLEVEC: [ty, opval, opval, opval]
1084 case bitc::FUNC_CODE_INST_CMP:
1085 // CMP: [opty, opval, opval, pred]
1086
1087 case bitc::FUNC_CODE_INST_RET:
1088 // RET: [opty,opval]
1089 case bitc::FUNC_CODE_INST_BR:
1090 // BR: [opval, bb#, bb#] or [bb#]
1091 case bitc::FUNC_CODE_INST_SWITCH:
1092 // SWITCH: [opty, opval, n, n x ops]
1093 case bitc::FUNC_CODE_INST_INVOKE:
1094 // INVOKE: [fnty, op0,op1,op2, ...]
1095 case bitc::FUNC_CODE_INST_UNWIND:
1096 // UNWIND
1097 case bitc::FUNC_CODE_INST_UNREACHABLE:
1098 // UNREACHABLE
1099
1100 case bitc::FUNC_CODE_INST_PHI:
1101 // PHI: [ty, #ops, val0,bb0, ...]
1102 case bitc::FUNC_CODE_INST_MALLOC:
1103 // MALLOC: [instty, op, align]
1104 case bitc::FUNC_CODE_INST_FREE:
1105 // FREE: [opty, op]
1106 case bitc::FUNC_CODE_INST_ALLOCA:
1107 // ALLOCA: [instty, op, align]
1108 case bitc::FUNC_CODE_INST_LOAD:
1109 // LOAD: [opty, op, align, vol]
1110 case bitc::FUNC_CODE_INST_STORE:
1111 // STORE: [ptrty,val,ptr, align, vol]
1112 case bitc::FUNC_CODE_INST_CALL:
1113 // CALL: [fnty, fnid, arg0, arg1...]
1114 case bitc::FUNC_CODE_INST_VAARG:
1115 // VAARG: [valistty, valist, instty]
1116 break;
1117 #endif
1118 }
1119
1120 // Add instruction to end of current BB. If there is no current BB, reject
1121 // this file.
1122 if (CurBB == 0) {
1123 delete I;
1124 return Error("Invalid instruction with no BB");
1125 }
1126 CurBB->getInstList().push_back(I);
1127
1128 // If this was a terminator instruction, move to the next block.
1129 if (isa(I)) {
1130 ++CurBBNo;
1131 CurBB = CurBBNo < FunctionBBs.size() ? FunctionBBs[CurBBNo] : 0;
1132 }
1133
1134 // Non-void values get registered in the value table for future use.
1135 if (I && I->getType() != Type::VoidTy)
1136 ValueList.AssignValue(I, NextValueNo++);
1137 }
1138
1139 // Check the function list for unresolved values.
1140 if (Argument *A = dyn_cast(ValueList.back())) {
1141 if (A->getParent() == 0) {
1142 // We found at least one unresolved value. Nuke them all to avoid leaks.
1143 for (unsigned i = ModuleValueListSize, e = ValueList.size(); i != e; ++i){
1144 if ((A = dyn_cast(ValueList.back())) && A->getParent() == 0) {
1145 A->replaceAllUsesWith(UndefValue::get(A->getType()));
1146 delete A;
1147 }
1148 }
1149 }
1150 return Error("Never resolved value found in function!");
1151 }
10461152
10471153 // Trim the value list down to the size it was before we parsed this function.
10481154 ValueList.shrinkTo(ModuleValueListSize);
5050 virtual void print(std::ostream&) const {}
5151
5252 Constant *getConstantFwdRef(unsigned Idx, const Type *Ty);
53 Value *getValueFwdRef(unsigned Idx, const Type *Ty);
54
55 void AssignValue(Value *V, unsigned Idx) {
56 if (Idx == size()) {
57 push_back(V);
58 } else if (Value *OldV = getOperand(Idx)) {
59 // If there was a forward reference to this value, replace it.
60 setOperand(Idx, V);
61 OldV->replaceAllUsesWith(V);
62 delete OldV;
63 } else {
64 initVal(Idx, V);
65 }
66 }
67
68 private:
5369 void initVal(unsigned Idx, Value *V) {
5470 assert(Uses[Idx] == 0 && "Cannot init an already init'd Use!");
5571 Uses[Idx].init(V, this);
112128 bool ParseBitcode();
113129 private:
114130 const Type *getTypeByID(unsigned ID, bool isTypeTable = false);
131 Value *getFnValueByID(unsigned ID, const Type *Ty) {
132 return ValueList.getValueFwdRef(ID, Ty);
133 }
115134
116135 bool ParseModule(const std::string &ModuleID);
117136 bool ParseTypeTable();