llvm.org GIT mirror llvm / 7337ab9
stop encoding type/value pairs when the type is implied by the value. This shrinks the function block of kc++ from 1055K to 906K git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@36816 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 12 years ago
3 changed file(s) with 152 addition(s) and 117 deletion(s). Raw diff Collapse all Expand all
11671167 break;
11681168 }
11691169 case bitc::FUNC_CODE_INST_GEP: { // GEP: [n x operands]
1170 if (Record.size() < 2 || (Record.size() & 1))
1170 unsigned OpNum = 0;
1171 Value *BasePtr;
1172 if (getValueTypePair(Record, OpNum, NextValueNo, BasePtr))
11711173 return Error("Invalid GEP record");
1172 const Type *OpTy = getTypeByID(Record[0]);
1173 Value *Op = getFnValueByID(Record[1], OpTy);
1174 if (OpTy == 0 || Op == 0)
1175 return Error("Invalid GEP record");
11761174
11771175 SmallVector GEPIdx;
1178 for (unsigned i = 1, e = Record.size()/2; i != e; ++i) {
1179 const Type *IdxTy = getTypeByID(Record[i*2]);
1180 Value *Idx = getFnValueByID(Record[i*2+1], IdxTy);
1181 if (IdxTy == 0 || Idx == 0)
1176 while (OpNum != Record.size()) {
1177 Value *Op;
1178 if (getValueTypePair(Record, OpNum, NextValueNo, Op))
11821179 return Error("Invalid GEP record");
1183 GEPIdx.push_back(Idx);
1184 }
1185
1186 I = new GetElementPtrInst(Op, &GEPIdx[0], GEPIdx.size());
1180 GEPIdx.push_back(Op);
1181 }
1182
1183 I = new GetElementPtrInst(BasePtr, &GEPIdx[0], GEPIdx.size());
11871184 break;
11881185 }
11891186
12411238 }
12421239
12431240 case bitc::FUNC_CODE_INST_CMP: { // CMP: [opty, opval, opval, pred]
1244 if (Record.size() < 4) return Error("Invalid CMP record");
1245 const Type *OpTy = getTypeByID(Record[0]);
1246 Value *LHS = getFnValueByID(Record[1], OpTy);
1247 Value *RHS = getFnValueByID(Record[2], OpTy);
1248 if (OpTy == 0 || LHS == 0 || RHS == 0)
1241 unsigned OpNum = 0;
1242 Value *LHS, *RHS;
1243 if (getValueTypePair(Record, OpNum, NextValueNo, LHS) ||
1244 getValue(Record, OpNum, LHS->getType(), RHS) ||
1245 OpNum+1 != Record.size())
12491246 return Error("Invalid CMP record");
1250 if (OpTy->isFPOrFPVector())
1251 I = new FCmpInst((FCmpInst::Predicate)Record[3], LHS, RHS);
1247
1248 if (LHS->getType()->isFPOrFPVector())
1249 I = new FCmpInst((FCmpInst::Predicate)Record[OpNum], LHS, RHS);
12521250 else
1253 I = new ICmpInst((ICmpInst::Predicate)Record[3], LHS, RHS);
1251 I = new ICmpInst((ICmpInst::Predicate)Record[OpNum], LHS, RHS);
12541252 break;
12551253 }
12561254
12581256 if (Record.size() == 0) {
12591257 I = new ReturnInst();
12601258 break;
1261 }
1262 if (Record.size() == 2) {
1263 const Type *OpTy = getTypeByID(Record[0]);
1264 Value *Op = getFnValueByID(Record[1], OpTy);
1265 if (!OpTy || !Op)
1259 } else {
1260 unsigned OpNum = 0;
1261 Value *Op;
1262 if (getValueTypePair(Record, OpNum, NextValueNo, Op) ||
1263 OpNum != Record.size())
12661264 return Error("Invalid RET record");
12671265 I = new ReturnInst(Op);
12681266 break;
12691267 }
1270 return Error("Invalid RET record");
12711268 case bitc::FUNC_CODE_INST_BR: { // BR: [bb#, bb#, opval] or [bb#]
12721269 if (Record.size() != 1 && Record.size() != 3)
12731270 return Error("Invalid BR record");
13111308 }
13121309
13131310 case bitc::FUNC_CODE_INST_INVOKE: { // INVOKE: [cc,fnty, op0,op1,op2, ...]
1314 if (Record.size() < 5)
1311 if (Record.size() < 3) return Error("Invalid INVOKE record");
1312 unsigned CCInfo = Record[0];
1313 BasicBlock *NormalBB = getBasicBlock(Record[1]);
1314 BasicBlock *UnwindBB = getBasicBlock(Record[2]);
1315
1316 unsigned OpNum = 3;
1317 Value *Callee;
1318 if (getValueTypePair(Record, OpNum, NextValueNo, Callee))
13151319 return Error("Invalid INVOKE record");
1316 unsigned CCInfo = Record[0];
1317 const PointerType *CalleeTy =
1318 dyn_cast_or_null(getTypeByID(Record[1]));
1319 Value *Callee = getFnValueByID(Record[2], CalleeTy);
1320 BasicBlock *NormalBB = getBasicBlock(Record[3]);
1321 BasicBlock *UnwindBB = getBasicBlock(Record[4]);
1322 if (CalleeTy == 0 || Callee == 0 || NormalBB == 0 || UnwindBB == 0)
1320
1321 const PointerType *CalleeTy = dyn_cast(Callee->getType());
1322 const FunctionType *FTy = !CalleeTy ? 0 :
1323 dyn_cast(CalleeTy->getElementType());
1324
1325 // Check that the right number of fixed parameters are here.
1326 if (FTy == 0 || NormalBB == 0 || UnwindBB == 0 ||
1327 Record.size() < OpNum+FTy->getNumParams())
13231328 return Error("Invalid INVOKE record");
13241329
1325 const FunctionType *FTy =
1326 dyn_cast(CalleeTy->getElementType());
1327
1328 // Check that the right number of fixed parameters are here.
1329 if (FTy == 0 || Record.size() < 5+FTy->getNumParams())
1330 return Error("Invalid INVOKE record");
1331
13321330 SmallVector Ops;
1333 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i) {
1334 Ops.push_back(getFnValueByID(Record[5+i], FTy->getParamType(i)));
1335 if (Ops.back() == 0)
1331 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i, ++OpNum) {
1332 Ops.push_back(getFnValueByID(Record[OpNum], FTy->getParamType(i)));
1333 if (Ops.back() == 0) return Error("Invalid INVOKE record");
1334 }
1335
1336 if (!FTy->isVarArg()) {
1337 if (Record.size() != OpNum)
13361338 return Error("Invalid INVOKE record");
1337 }
1338
1339 unsigned FirstVarargParam = 5+FTy->getNumParams();
1340 if (FTy->isVarArg()) {
1339 } else {
13411340 // Read type/value pairs for varargs params.
1342 if ((Record.size()-FirstVarargParam) & 1)
1343 return Error("Invalid INVOKE record");
1344
1345 for (unsigned i = FirstVarargParam, e = Record.size(); i != e; i += 2) {
1346 const Type *ArgTy = getTypeByID(Record[i]);
1347 Ops.push_back(getFnValueByID(Record[i+1], ArgTy));
1348 if (Ops.back() == 0 || ArgTy == 0)
1341 while (OpNum != Record.size()) {
1342 Value *Op;
1343 if (getValueTypePair(Record, OpNum, NextValueNo, Op))
13491344 return Error("Invalid INVOKE record");
1345 Ops.push_back(Op);
13501346 }
1351 } else {
1352 if (Record.size() != FirstVarargParam)
1353 return Error("Invalid INVOKE record");
13541347 }
13551348
13561349 I = new InvokeInst(Callee, NormalBB, UnwindBB, &Ops[0], Ops.size());
14151408 break;
14161409 }
14171410 case bitc::FUNC_CODE_INST_LOAD: { // LOAD: [opty, op, align, vol]
1418 if (Record.size() < 4)
1419 return Error("Invalid LOAD record");
1420 const Type *OpTy = getTypeByID(Record[0]);
1421 Value *Op = getFnValueByID(Record[1], OpTy);
1422 if (!OpTy || !Op)
1423 return Error("Invalid LOAD record");
1424 I = new LoadInst(Op, "", Record[3], (1 << Record[2]) >> 1);
1411 unsigned OpNum = 0;
1412 Value *Op;
1413 if (getValueTypePair(Record, OpNum, NextValueNo, Op) ||
1414 OpNum+2 != Record.size())
1415 return Error("Invalid RET record");
1416
1417 I = new LoadInst(Op, "", Record[OpNum+1], (1 << Record[OpNum]) >> 1);
14251418 break;
14261419 }
14271420 case bitc::FUNC_CODE_INST_STORE: { // STORE:[ptrty,val,ptr, align, vol]
14371430 break;
14381431 }
14391432 case bitc::FUNC_CODE_INST_CALL: { // CALL: [cc, fnty, fnid, arg0, arg1...]
1440 if (Record.size() < 3)
1433 if (Record.size() < 1)
14411434 return Error("Invalid CALL record");
14421435 unsigned CCInfo = Record[0];
1443 const PointerType *OpTy =
1444 dyn_cast_or_null(getTypeByID(Record[1]));
1436
1437 unsigned OpNum = 1;
1438 Value *Callee;
1439 if (getValueTypePair(Record, OpNum, NextValueNo, Callee))
1440 return Error("Invalid CALL record");
1441
1442 const PointerType *OpTy = dyn_cast(Callee->getType());
14451443 const FunctionType *FTy = 0;
14461444 if (OpTy) FTy = dyn_cast(OpTy->getElementType());
1447 Value *Callee = getFnValueByID(Record[2], OpTy);
1448 if (!FTy || !Callee || Record.size() < FTy->getNumParams()+3)
1445 if (!FTy || Record.size() < FTy->getNumParams()+OpNum)
14491446 return Error("Invalid CALL record");
14501447
14511448 SmallVector Args;
14521449 // Read the fixed params.
1453 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i) {
1454 Args.push_back(getFnValueByID(Record[i+3], FTy->getParamType(i)));
1450 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i, ++OpNum) {
1451 Args.push_back(getFnValueByID(Record[OpNum], FTy->getParamType(i)));
14551452 if (Args.back() == 0) return Error("Invalid CALL record");
14561453 }
14571454
1458
14591455 // Read type/value pairs for varargs params.
1460 unsigned NextArg = FTy->getNumParams()+3;
14611456 if (!FTy->isVarArg()) {
1462 if (NextArg != Record.size())
1457 if (OpNum != Record.size())
14631458 return Error("Invalid CALL record");
14641459 } else {
1465 if ((Record.size()-NextArg) & 1)
1466 return Error("Invalid CALL record");
1467 for (unsigned e = Record.size(); NextArg != e; NextArg += 2) {
1468 Args.push_back(getFnValueByID(Record[NextArg+1],
1469 getTypeByID(Record[NextArg])));
1470 if (Args.back() == 0) return Error("Invalid CALL record");
1460 while (OpNum != Record.size()) {
1461 Value *Op;
1462 if (getValueTypePair(Record, OpNum, NextValueNo, Op))
1463 return Error("Invalid CALL record");
1464 Args.push_back(Op);
14711465 }
14721466 }
14731467
146146 return ParamAttrs[i-1];
147147 return 0;
148148 }
149
150 /// getValueTypePair - Read a value/type pair out of the specified record from
151 /// slot 'Slot'. Increment Slot past the number of slots used in the record.
152 /// Return true on failure.
153 bool getValueTypePair(SmallVector &Record, unsigned &Slot,
154 unsigned InstNum, Value *&ResVal) {
155 if (Slot == Record.size()) return true;
156 unsigned ValNo = Record[Slot++];
157 if (ValNo < InstNum) {
158 // If this is not a forward reference, just return the value we already
159 // have.
160 ResVal = getFnValueByID(ValNo, 0);
161 return ResVal == 0;
162 } else if (Slot == Record.size()) {
163 return true;
164 }
165
166 unsigned TypeNo = Record[Slot++];
167 ResVal = getFnValueByID(ValNo, getTypeByID(TypeNo));
168 return ResVal == 0;
169 }
170 bool getValue(SmallVector &Record, unsigned &Slot,
171 const Type *Ty, Value *&ResVal) {
172 if (Slot == Record.size()) return true;
173 unsigned ValNo = Record[Slot++];
174 ResVal = getFnValueByID(ValNo, Ty);
175 return ResVal == 0;
176 }
149177
150178
151179 bool ParseModule(const std::string &ModuleID);
571571 }
572572 }
573573
574 /// PushValueAndType - The file has to encode both the value and type id for
575 /// many values, because we need to know what type to create for forward
576 /// references. However, most operands are not forward references, so this type
577 /// field is not needed.
578 ///
579 /// This function adds V's value ID to Vals. If the value ID is higher than the
580 /// instruction ID, then it is a forward reference, and it also includes the
581 /// type ID.
582 static bool PushValueAndType(Value *V, unsigned InstID,
583 SmallVector &Vals,
584 ValueEnumerator &VE) {
585 unsigned ValID = VE.getValueID(V);
586 Vals.push_back(ValID);
587 if (ValID >= InstID) {
588 Vals.push_back(VE.getTypeID(V->getType()));
589 return true;
590 }
591 return false;
592 }
593
574594 /// WriteInstruction - Emit an instruction to the specified stream.
575 static void WriteInstruction(const Instruction &I, ValueEnumerator &VE,
576 BitstreamWriter &Stream,
595 static void WriteInstruction(const Instruction &I, unsigned InstID,
596 ValueEnumerator &VE, BitstreamWriter &Stream,
577597 SmallVector &Vals) {
578598 unsigned Code = 0;
579599 unsigned AbbrevToUse = 0;
597617
598618 case Instruction::GetElementPtr:
599619 Code = bitc::FUNC_CODE_INST_GEP;
600 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) {
601 Vals.push_back(VE.getTypeID(I.getOperand(i)->getType()));
602 Vals.push_back(VE.getValueID(I.getOperand(i)));
603 }
620 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
621 PushValueAndType(I.getOperand(i), InstID, Vals, VE);
604622 break;
605623 case Instruction::Select:
606624 Code = bitc::FUNC_CODE_INST_SELECT;
632650 case Instruction::ICmp:
633651 case Instruction::FCmp:
634652 Code = bitc::FUNC_CODE_INST_CMP;
635 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));
636 Vals.push_back(VE.getValueID(I.getOperand(0)));
653 PushValueAndType(I.getOperand(0), InstID, Vals, VE);
637654 Vals.push_back(VE.getValueID(I.getOperand(1)));
638655 Vals.push_back(cast(I).getPredicate());
639656 break;
640657
641658 case Instruction::Ret:
642659 Code = bitc::FUNC_CODE_INST_RET;
643 if (I.getNumOperands()) {
644 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));
645 Vals.push_back(VE.getValueID(I.getOperand(0)));
646 }
660 if (I.getNumOperands())
661 PushValueAndType(I.getOperand(0), InstID, Vals, VE);
647662 break;
648663 case Instruction::Br:
649664 Code = bitc::FUNC_CODE_INST_BR;
662677 case Instruction::Invoke: {
663678 Code = bitc::FUNC_CODE_INST_INVOKE;
664679 Vals.push_back(cast(I).getCallingConv());
665 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));
666 Vals.push_back(VE.getValueID(I.getOperand(0))); // callee
667 Vals.push_back(VE.getValueID(I.getOperand(1))); // normal
668 Vals.push_back(VE.getValueID(I.getOperand(2))); // unwind
680 Vals.push_back(VE.getValueID(I.getOperand(1))); // normal dest
681 Vals.push_back(VE.getValueID(I.getOperand(2))); // unwind dest
682 PushValueAndType(I.getOperand(0), InstID, Vals, VE); // callee
669683
670684 // Emit value #'s for the fixed parameters.
671685 const PointerType *PTy = cast(I.getOperand(0)->getType());
675689
676690 // Emit type/value pairs for varargs params.
677691 if (FTy->isVarArg()) {
678 unsigned NumVarargs = I.getNumOperands()-3-FTy->getNumParams();
679 for (unsigned i = I.getNumOperands()-NumVarargs, e = I.getNumOperands();
680 i != e; ++i) {
681 Vals.push_back(VE.getTypeID(I.getOperand(i)->getType()));
682 Vals.push_back(VE.getValueID(I.getOperand(i)));
683 }
692 for (unsigned i = 3+FTy->getNumParams(), e = I.getNumOperands();
693 i != e; ++i)
694 PushValueAndType(I.getOperand(i), InstID, Vals, VE); // vararg
684695 }
685696 break;
686697 }
720731
721732 case Instruction::Load:
722733 Code = bitc::FUNC_CODE_INST_LOAD;
723 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));
724 Vals.push_back(VE.getValueID(I.getOperand(0))); // ptr.
734 if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE)) // ptr
735 AbbrevToUse = FUNCTION_INST_LOAD_ABBREV;
736
725737 Vals.push_back(Log2_32(cast(I).getAlignment())+1);
726738 Vals.push_back(cast(I).isVolatile());
727 AbbrevToUse = FUNCTION_INST_LOAD_ABBREV;
728739 break;
729740 case Instruction::Store:
730741 Code = bitc::FUNC_CODE_INST_STORE;
738749 Code = bitc::FUNC_CODE_INST_CALL;
739750 Vals.push_back((cast(I).getCallingConv() << 1) |
740751 cast(I).isTailCall());
741 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));
742 Vals.push_back(VE.getValueID(I.getOperand(0))); // callee
752 PushValueAndType(I.getOperand(0), InstID, Vals, VE); // Callee
743753
744754 // Emit value #'s for the fixed parameters.
745755 const PointerType *PTy = cast(I.getOperand(0)->getType());
751761 if (FTy->isVarArg()) {
752762 unsigned NumVarargs = I.getNumOperands()-1-FTy->getNumParams();
753763 for (unsigned i = I.getNumOperands()-NumVarargs, e = I.getNumOperands();
754 i != e; ++i) {
755 Vals.push_back(VE.getTypeID(I.getOperand(i)->getType()));
756 Vals.push_back(VE.getValueID(I.getOperand(i)));
757 }
764 i != e; ++i)
765 PushValueAndType(I.getOperand(i), InstID, Vals, VE); // varargs
758766 }
759767 break;
760768 }
849857 VE.getFunctionConstantRange(CstStart, CstEnd);
850858 WriteConstants(CstStart, CstEnd, VE, Stream, false);
851859
860 // Keep a running idea of what the instruction ID is.
861 unsigned InstID = CstEnd;
862
852863 // Finally, emit all the instructions, in order.
853864 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
854 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
855 WriteInstruction(*I, VE, Stream, Vals);
865 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end();
866 I != E; ++I) {
867 WriteInstruction(*I, InstID, VE, Stream, Vals);
868 if (I->getType() != Type::VoidTy)
869 ++InstID;
870 }
856871
857872 // Emit names for all the instructions etc.
858873 WriteValueSymbolTable(F.getValueSymbolTable(), VE, Stream);
9961011 { // INST_LOAD abbrev for FUNCTION_BLOCK.
9971012 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
9981013 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_LOAD));
999 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // typeid
1000 Log2_32_Ceil(VE.getTypes().size()+1)));
10011014 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Ptr
10021015 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // Align
10031016 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // volatile