llvm.org GIT mirror llvm / b8f7479
Update GEP constructors to use an iterator interface to fix GLIBCXX_DEBUG issues. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@41697 91177308-0d34-0410-b5e6-96231b3b80d8 David Greene 13 years ago
21 changed file(s) with 195 addition(s) and 160 deletion(s). Raw diff Collapse all Expand all
366366 // GetElementPtrInst Class
367367 //===----------------------------------------------------------------------===//
368368
369 // checkType - Simple wrapper function to give a better assertion failure
370 // message on bad indexes for a gep instruction.
371 //
372 static inline const Type *checkType(const Type *Ty) {
373 assert(Ty && "Invalid GetElementPtrInst indices for type!");
374 return Ty;
375 }
376
369377 /// GetElementPtrInst - an instruction for type-safe pointer arithmetic to
370378 /// access elements of arrays and structs
371379 ///
379387 OL[i].init(GEPIOL[i], this);
380388 }
381389 void init(Value *Ptr, Value* const *Idx, unsigned NumIdx);
382 void init(Value *Ptr, Value *Idx0, Value *Idx1);
383390 void init(Value *Ptr, Value *Idx);
391
392 template
393 void init(Value *Ptr, InputIterator IdxBegin, InputIterator IdxEnd,
394 const std::string &Name,
395 // This argument ensures that we have an iterator we can
396 // do arithmetic on in constant time
397 std::random_access_iterator_tag) {
398 typename std::iterator_traits::difference_type NumIdx =
399 std::distance(IdxBegin, IdxEnd);
400
401 if (NumIdx > 0) {
402 // This requires that the itoerator points to contiguous memory.
403 init(Ptr, &*IdxBegin, NumIdx);
404 }
405 else {
406 init(Ptr, 0, NumIdx);
407 }
408
409 setName(Name);
410 }
411
412 /// getIndexedType - Returns the type of the element that would be loaded with
413 /// a load instruction with the specified parameters.
414 ///
415 /// A null type is returned if the indices are invalid for the specified
416 /// pointer type.
417 ///
418 static const Type *getIndexedType(const Type *Ptr,
419 Value* const *Idx, unsigned NumIdx,
420 bool AllowStructLeaf = false);
421
422 template
423 static const Type *getIndexedType(const Type *Ptr,
424 InputIterator IdxBegin,
425 InputIterator IdxEnd,
426 bool AllowStructLeaf,
427 // This argument ensures that we
428 // have an iterator we can do
429 // arithmetic on in constant time
430 std::random_access_iterator_tag) {
431 typename std::iterator_traits::difference_type NumIdx =
432 std::distance(IdxBegin, IdxEnd);
433
434 if (NumIdx > 0) {
435 // This requires that the iterator points to contiguous memory.
436 return(getIndexedType(Ptr, (Value *const *)&*IdxBegin, NumIdx,
437 AllowStructLeaf));
438 }
439 else {
440 return(getIndexedType(Ptr, (Value *const*)0, NumIdx, AllowStructLeaf));
441 }
442 }
443
384444 public:
385445 /// Constructors - Create a getelementptr instruction with a base pointer an
386446 /// list of indices. The first ctor can optionally insert before an existing
387447 /// instruction, the second appends the new instruction to the specified
388448 /// BasicBlock.
389 GetElementPtrInst(Value *Ptr, Value* const *Idx, unsigned NumIdx,
390 const std::string &Name = "", Instruction *InsertBefore =0);
391 GetElementPtrInst(Value *Ptr, Value* const *Idx, unsigned NumIdx,
392 const std::string &Name, BasicBlock *InsertAtEnd);
393
449 template
450 GetElementPtrInst(Value *Ptr, InputIterator IdxBegin,
451 InputIterator IdxEnd,
452 const std::string &Name = "",
453 Instruction *InsertBefore =0)
454 : Instruction(PointerType::get(
455 checkType(getIndexedType(Ptr->getType(),
456 IdxBegin, IdxEnd, true))),
457 GetElementPtr, 0, 0, InsertBefore) {
458 init(Ptr, IdxBegin, IdxEnd, Name,
459 typename std::iterator_traits::iterator_category());
460 }
461 template
462 GetElementPtrInst(Value *Ptr, InputIterator IdxBegin, InputIterator IdxEnd,
463 const std::string &Name, BasicBlock *InsertAtEnd)
464 : Instruction(PointerType::get(
465 checkType(getIndexedType(Ptr->getType(),
466 IdxBegin, IdxEnd, true))),
467 GetElementPtr, 0, 0, InsertAtEnd) {
468 init(Ptr, IdxBegin, IdxEnd, Name,
469 typename std::iterator_traits::iterator_category());
470 }
471
394472 /// Constructors - These two constructors are convenience methods because one
395473 /// and two index getelementptr instructions are so common.
396474 GetElementPtrInst(Value *Ptr, Value *Idx,
397475 const std::string &Name = "", Instruction *InsertBefore =0);
398476 GetElementPtrInst(Value *Ptr, Value *Idx,
399477 const std::string &Name, BasicBlock *InsertAtEnd);
400 GetElementPtrInst(Value *Ptr, Value *Idx0, Value *Idx1,
401 const std::string &Name = "", Instruction *InsertBefore =0);
402 GetElementPtrInst(Value *Ptr, Value *Idx0, Value *Idx1,
403 const std::string &Name, BasicBlock *InsertAtEnd);
404478 ~GetElementPtrInst();
405479
406480 virtual GetElementPtrInst *clone() const;
416490 /// A null type is returned if the indices are invalid for the specified
417491 /// pointer type.
418492 ///
493 template
419494 static const Type *getIndexedType(const Type *Ptr,
420 Value* const *Idx, unsigned NumIdx,
421 bool AllowStructLeaf = false);
422
423 static const Type *getIndexedType(const Type *Ptr, Value *Idx0, Value *Idx1,
424 bool AllowStructLeaf = false);
495 InputIterator IdxBegin,
496 InputIterator IdxEnd,
497 bool AllowStructLeaf = false) {
498 return(getIndexedType(Ptr, IdxBegin, IdxEnd, AllowStructLeaf,
499 typename std::iterator_traits::
500 iterator_category()));
501 }
425502 static const Type *getIndexedType(const Type *Ptr, Value *Idx);
426503
427504 inline op_iterator idx_begin() { return op_begin()+1; }
216216 StoreInst *CreateStore(Value *Val, Value *Ptr, bool isVolatile = false) {
217217 return Insert(new StoreInst(Val, Ptr, isVolatile));
218218 }
219 GetElementPtrInst *CreateGEP(Value *Ptr, Value* const *Idx, unsigned NumIdx,
220 const char *Name = "") {
221 return Insert(new GetElementPtrInst(Ptr, Idx, NumIdx, Name));
219 template
220 GetElementPtrInst *CreateGEP(Value *Ptr, InputIterator IdxBegin,
221 InputIterator IdxEnd, const char *Name = "") {
222 return(Insert(new GetElementPtrInst(Ptr, IdxBegin, IdxEnd, Name)));
222223 }
223224 GetElementPtrInst *CreateGEP(Value *Ptr, Value *Idx, const char *Name = "") {
224 return Insert(new GetElementPtrInst(Ptr, &Idx, 1, Name));
225 }
226 GetElementPtrInst *CreateGEP(Value *Ptr, Value *Idx0, Value *Idx1,
227 const char *Name = "") {
228 return Insert(new GetElementPtrInst(Ptr, Idx0, Idx1, Name));
225 return Insert(new GetElementPtrInst(Ptr, Idx, Name));
229226 }
230227
231228 //===--------------------------------------------------------------------===//
18851885 GEN_ERROR("GetElementPtr requires a pointer operand");
18861886
18871887 const Type *IdxTy =
1888 GetElementPtrInst::getIndexedType($3->getType(), &(*$4)[0], $4->size(),
1888 GetElementPtrInst::getIndexedType($3->getType(), $4->begin(), $4->end(),
18891889 true);
18901890 if (!IdxTy)
18911891 GEN_ERROR("Index list invalid for constant getelementptr");
30633063 if (!isa($2->get()))
30643064 GEN_ERROR("getelementptr insn requires pointer operand");
30653065
3066 if (!GetElementPtrInst::getIndexedType(*$2, &(*$4)[0], $4->size(), true))
3066 if (!GetElementPtrInst::getIndexedType(*$2, $4->begin(), $4->end(), true))
30673067 GEN_ERROR("Invalid getelementptr indices for type '" +
30683068 (*$2)->getDescription()+ "'");
30693069 Value* tmpVal = getVal(*$2, $3);
30703070 CHECK_FOR_ERROR
3071 $$ = new GetElementPtrInst(tmpVal, &(*$4)[0], $4->size());
3071 $$ = new GetElementPtrInst(tmpVal, $4->begin(), $4->end());
30723072 delete $2;
30733073 delete $4;
30743074 };
12331233 GEPIdx.push_back(Op);
12341234 }
12351235
1236 I = new GetElementPtrInst(BasePtr, &GEPIdx[0], GEPIdx.size());
1236 I = new GetElementPtrInst(BasePtr, GEPIdx.begin(), GEPIdx.end());
12371237 break;
12381238 }
12391239
375375 for (ScalarizeTable::iterator SI = ArgIndices.begin(),
376376 E = ArgIndices.end(); SI != E; ++SI)
377377 Params.push_back(GetElementPtrInst::getIndexedType(I->getType(),
378 &(*SI)[0],
379 SI->size()));
378 SI->begin(),
379 SI->end()));
380380
381381 if (ArgIndices.size() == 1 && ArgIndices.begin()->empty())
382382 ++NumArgumentsPromoted;
427427 Value *V = *AI;
428428 LoadInst *OrigLoad = OriginalLoads[*SI];
429429 if (!SI->empty()) {
430 V = new GetElementPtrInst(V, &(*SI)[0], SI->size(),
430 V = new GetElementPtrInst(V, SI->begin(), SI->end(),
431431 V->getName()+".idx", Call);
432432 AA.copyValue(OrigLoad->getOperand(0), V);
433433 }
463463 Idxs.push_back(NullInt);
464464 for (unsigned i = 3, e = GEPI->getNumOperands(); i != e; ++i)
465465 Idxs.push_back(GEPI->getOperand(i));
466 NewPtr = new GetElementPtrInst(NewPtr, &Idxs[0], Idxs.size(),
466 NewPtr = new GetElementPtrInst(NewPtr, Idxs.begin(), Idxs.end(),
467467 GEPI->getName()+"."+utostr(Val), GEPI);
468468 }
469469 GEP->replaceAllUsesWith(NewPtr);
697697 MI->getAlignment(), MI->getName(), MI);
698698 Value* Indices[2];
699699 Indices[0] = Indices[1] = Constant::getNullValue(Type::Int32Ty);
700 Value *NewGEP = new GetElementPtrInst(NewMI, Indices, 2,
700 Value *NewGEP = new GetElementPtrInst(NewMI, Indices, Indices + 2,
701701 NewMI->getName()+".el0", MI);
702702 MI->replaceAllUsesWith(NewGEP);
703703 MI->eraseFromParent();
925925 GEPIdx.push_back(GEPI->getOperand(1));
926926 GEPIdx.append(GEPI->op_begin()+3, GEPI->op_end());
927927
928 Value *NGEPI = new GetElementPtrInst(NewPtr, &GEPIdx[0], GEPIdx.size(),
928 Value *NGEPI = new GetElementPtrInst(NewPtr, GEPIdx.begin(), GEPIdx.end(),
929929 GEPI->getName(), GEPI);
930930 GEPI->replaceAllUsesWith(NGEPI);
931931 GEPI->eraseFromParent();
944944
945945 if (newOp1 != U->getPointerOperand() || changed_idx) {
946946 Instruction* newVal = new GetElementPtrInst(newOp1,
947 &newIdx[0], newIdx.size(),
947 newIdx.begin(), newIdx.end(),
948948 U->getName()+".expr");
949949
950950 uint32_t v = VN.lookup_or_add(newVal);
16741674 C->getName()+".gvnpre",
16751675 (*PI)->getTerminator());
16761676 else if (GetElementPtrInst* G = dyn_cast(U))
1677 newVal = new GetElementPtrInst(s1, &sVarargs[0], sVarargs.size(),
1677 newVal = new GetElementPtrInst(s1, sVarargs.begin(), sVarargs.end(),
16781678 G->getName()+".gvnpre",
16791679 (*PI)->getTerminator());
16801680
177177 Constant *NCE = ConstantExpr::getGetElementPtr(CE->getOperand(0),
178178 &CEIdxs[0],
179179 CEIdxs.size());
180 Value *Idx[2];
181 Idx[0] = Constant::getNullValue(Type::Int32Ty);
182 Idx[1] = NewAdd;
180183 GetElementPtrInst *NGEPI = new GetElementPtrInst(
181 NCE, Constant::getNullValue(Type::Int32Ty), NewAdd,
184 NCE, Idx, Idx + 2,
182185 GEPI->getName(), GEPI);
183186 SE->deleteValueFromRecords(GEPI);
184187 GEPI->replaceAllUsesWith(NGEPI);
65636563 // If we were able to index down into an element, create the GEP
65646564 // and bitcast the result. This eliminates one bitcast, potentially
65656565 // two.
6566 Instruction *NGEP = new GetElementPtrInst(OrigBase, &NewIndices[0],
6567 NewIndices.size(), "");
6566 Instruction *NGEP = new GetElementPtrInst(OrigBase,
6567 NewIndices.begin(),
6568 NewIndices.end(), "");
65686569 InsertNewInstBefore(NGEP, CI);
65696570 NGEP->takeName(GEP);
65706571
70567057 // If we found a path from the src to dest, create the getelementptr now.
70577058 if (SrcElTy == DstElTy) {
70587059 SmallVector Idxs(NumZeros+1, ZeroUInt);
7059 return new GetElementPtrInst(Src, &Idxs[0], Idxs.size());
7060 return new GetElementPtrInst(Src, Idxs.begin(), Idxs.end());
70607061 }
70617062 }
70627063
85018502 }
85028503
85038504 if (!Indices.empty())
8504 return new GetElementPtrInst(SrcGEPOperands[0], &Indices[0],
8505 Indices.size(), GEP.getName());
8505 return new GetElementPtrInst(SrcGEPOperands[0], Indices.begin(),
8506 Indices.end(), GEP.getName());
85068507
85078508 } else if (GlobalValue *GV = dyn_cast(PtrOp)) {
85088509 // GEP of global variable. If all of the indices for this GEP are
85538554 if (isa(SrcElTy) &&
85548555 TD->getTypeSize(cast(SrcElTy)->getElementType()) ==
85558556 TD->getTypeSize(ResElTy)) {
8557 Value *Idx[2];
8558 Idx[0] = Constant::getNullValue(Type::Int32Ty);
8559 Idx[1] = GEP.getOperand(1);
85568560 Value *V = InsertNewInstBefore(
8557 new GetElementPtrInst(X, Constant::getNullValue(Type::Int32Ty),
8558 GEP.getOperand(1), GEP.getName()), GEP);
8561 new GetElementPtrInst(X, Idx, Idx + 2, GEP.getName()), GEP);
85598562 // V and GEP are both pointer types --> BitCast
85608563 return new BitCastInst(V, GEP.getType());
85618564 }
86088611 }
86098612
86108613 // Insert the new GEP instruction.
8614 Value *Idx[2];
8615 Idx[0] = Constant::getNullValue(Type::Int32Ty);
8616 Idx[1] = NewIdx;
86118617 Instruction *NewGEP =
8612 new GetElementPtrInst(X, Constant::getNullValue(Type::Int32Ty),
8613 NewIdx, GEP.getName());
8618 new GetElementPtrInst(X, Idx, Idx + 2, GEP.getName());
86148619 NewGEP = InsertNewInstBefore(NewGEP, GEP);
86158620 // The NewGEP must be pointer typed, so must the old one -> BitCast
86168621 return new BitCastInst(NewGEP, GEP.getType());
86508655 // insert our getelementptr instruction...
86518656 //
86528657 Value *NullIdx = Constant::getNullValue(Type::Int32Ty);
8653 Value *V = new GetElementPtrInst(New, NullIdx, NullIdx,
8658 Value *Idx[2];
8659 Idx[0] = NullIdx;
8660 Idx[1] = NullIdx;
8661 Value *V = new GetElementPtrInst(New, Idx, Idx + 2,
86548662 New->getName()+".sub", It);
86558663
86568664 // Now make everything use the getelementptr instead of the original
241241 Constant *Zero = ConstantInt::get(Type::Int32Ty, 0);
242242 Constant *One = ConstantInt::get(Type::Int32Ty, 1);
243243
244 Value *Idx[2] = { Zero, Zero };
245
244246 // Get a pointer to the prev pointer.
245 Value *PrevPtrPtr = new GetElementPtrInst(AI, Zero, Zero, "prevptrptr", IP);
247 Value *PrevPtrPtr = new GetElementPtrInst(AI, Idx, Idx + 2,
248 "prevptrptr", IP);
246249
247250 // Load the previous pointer.
248251 Value *PrevPtr = new LoadInst(RootChain, "prevptr", IP);
250253 new StoreInst(PrevPtr, PrevPtrPtr, IP);
251254
252255 // Set the number of elements in this record.
253 Value *NumEltsPtr = new GetElementPtrInst(AI, Zero, One, "numeltsptr", IP);
256 Idx[1] = One;
257 Value *NumEltsPtr = new GetElementPtrInst(AI, Idx, Idx + 2,
258 "numeltsptr", IP);
254259 new StoreInst(ConstantInt::get(Type::Int32Ty, GCRoots.size()), NumEltsPtr,IP);
255260
256261 Value* Par[4];
266271 // Initialize the meta-data pointer.
267272 Par[2] = ConstantInt::get(Type::Int32Ty, i);
268273 Par[3] = One;
269 Value *MetaDataPtr = new GetElementPtrInst(AI, Par, 4, "MetaDataPtr", IP);
274 Value *MetaDataPtr = new GetElementPtrInst(AI, Par, Par + 4,
275 "MetaDataPtr", IP);
270276 assert(isa(GCRoots[i]->getOperand(2)) && "Must be a constant");
271277 new StoreInst(GCRoots[i]->getOperand(2), MetaDataPtr, IP);
272278
273279 // Initialize the root pointer to null on entry to the function.
274280 Par[3] = Zero;
275 Value *RootPtrPtr = new GetElementPtrInst(AI, Par, 4, "RootEntPtr", IP);
281 Value *RootPtrPtr = new GetElementPtrInst(AI, Par, Par + 4,
282 "RootEntPtr", IP);
276283 new StoreInst(Null, RootPtrPtr, IP);
277284
278285 // Each occurrance of the llvm.gcroot intrinsic now turns into an
234234
235235 // Get the pointer
236236 Value* val = new GetElementPtrInst(array,
237 &Idx[0], Idx.size(),
237 Idx.begin(), Idx.end(),
238238 LI.getName() +
239239 ".ge." + utostr(i),
240240 &LI);
332332 // Generate the indices for getelementptr
333333 Idx[1] = ConstantInt::get(Type::Int32Ty,i);
334334 Value* val = new GetElementPtrInst(array,
335 &Idx[0], Idx.size(),
335 Idx.begin(), Idx.end(),
336336 "store.ge." +
337337 utostr(i) + ".",
338338 &SI);
318318 SmallVector NewArgs;
319319 NewArgs.push_back(Constant::getNullValue(Type::Int32Ty));
320320 NewArgs.append(GEPI->op_begin()+3, GEPI->op_end());
321 RepValue = new GetElementPtrInst(AllocaToUse, &NewArgs[0],
322 NewArgs.size(), "", GEPI);
321 RepValue = new GetElementPtrInst(AllocaToUse, NewArgs.begin(),
322 NewArgs.end(), "", GEPI);
323323 RepValue->takeName(GEPI);
324324 }
325325
625625 // If this is a memcpy/memmove, emit a GEP of the other element address.
626626 Value *OtherElt = 0;
627627 if (OtherPtr) {
628 OtherElt = new GetElementPtrInst(OtherPtr, Zero,
629 ConstantInt::get(Type::Int32Ty, i),
628 Value *Idx[2];
629 Idx[0] = Zero;
630 Idx[1] = ConstantInt::get(Type::Int32Ty, i);
631 OtherElt = new GetElementPtrInst(OtherPtr, Idx, Idx + 2,
630632 OtherPtr->getNameStr()+"."+utostr(i),
631633 MI);
632634 }
828830 SmallVector Indices(GEPI->op_begin()+1, GEPI->op_end());
829831 Indices[1] = Constant::getNullValue(Type::Int32Ty);
830832 Value *ZeroIdx = new GetElementPtrInst(GEPI->getOperand(0),
831 &Indices[0], Indices.size(),
833 Indices.begin(),
834 Indices.end(),
832835 GEPI->getName()+".0", GEPI);
833836 Indices[1] = ConstantInt::get(Type::Int32Ty, 1);
834837 Value *OneIdx = new GetElementPtrInst(GEPI->getOperand(0),
835 &Indices[0], Indices.size(),
838 Indices.begin(),
839 Indices.end(),
836840 GEPI->getName()+".1", GEPI);
837841 // Replace all loads of the variable index GEP with loads from both
838842 // indexes and a select.
293293 for (unsigned i = 0, e = inputs.size(); i != e; ++i) {
294294 Value *RewriteVal;
295295 if (AggregateArgs) {
296 Value *Idx0 = Constant::getNullValue(Type::Int32Ty);
297 Value *Idx1 = ConstantInt::get(Type::Int32Ty, i);
296 Value *Idx[2];
297 Idx[0] = Constant::getNullValue(Type::Int32Ty);
298 Idx[1] = ConstantInt::get(Type::Int32Ty, i);
298299 std::string GEPname = "gep_" + inputs[i]->getName();
299300 TerminatorInst *TI = newFunction->begin()->getTerminator();
300 GetElementPtrInst *GEP = new GetElementPtrInst(AI, Idx0, Idx1,
301 GetElementPtrInst *GEP = new GetElementPtrInst(AI, Idx, Idx+2,
301302 GEPname, TI);
302303 RewriteVal = new LoadInst(GEP, "load" + GEPname, TI);
303304 } else
380381 params.push_back(Struct);
381382
382383 for (unsigned i = 0, e = inputs.size(); i != e; ++i) {
383 Value *Idx0 = Constant::getNullValue(Type::Int32Ty);
384 Value *Idx1 = ConstantInt::get(Type::Int32Ty, i);
384 Value *Idx[2];
385 Idx[0] = Constant::getNullValue(Type::Int32Ty);
386 Idx[1] = ConstantInt::get(Type::Int32Ty, i);
385387 GetElementPtrInst *GEP =
386 new GetElementPtrInst(Struct, Idx0, Idx1,
388 new GetElementPtrInst(Struct, Idx, Idx + 2,
387389 "gep_" + StructValues[i]->getName());
388390 codeReplacer->getInstList().push_back(GEP);
389391 StoreInst *SI = new StoreInst(StructValues[i], GEP);
405407 for (unsigned i = 0, e = outputs.size(); i != e; ++i) {
406408 Value *Output = 0;
407409 if (AggregateArgs) {
408 Value *Idx0 = Constant::getNullValue(Type::Int32Ty);
409 Value *Idx1 = ConstantInt::get(Type::Int32Ty, FirstOut + i);
410 Value *Idx[2];
411 Idx[0] = Constant::getNullValue(Type::Int32Ty);
412 Idx[1] = ConstantInt::get(Type::Int32Ty, FirstOut + i);
410413 GetElementPtrInst *GEP
411 = new GetElementPtrInst(Struct, Idx0, Idx1,
414 = new GetElementPtrInst(Struct, Idx, Idx + 2,
412415 "gep_reload_" + outputs[i]->getName());
413416 codeReplacer->getInstList().push_back(GEP);
414417 Output = GEP;
505508
506509 if (DominatesDef) {
507510 if (AggregateArgs) {
508 Value *Idx0 = Constant::getNullValue(Type::Int32Ty);
509 Value *Idx1 = ConstantInt::get(Type::Int32Ty,FirstOut+out);
511 Value *Idx[2];
512 Idx[0] = Constant::getNullValue(Type::Int32Ty);
513 Idx[1] = ConstantInt::get(Type::Int32Ty,FirstOut+out);
510514 GetElementPtrInst *GEP =
511 new GetElementPtrInst(OAI, Idx0, Idx1,
515 new GetElementPtrInst(OAI, Idx, Idx + 2,
512516 "gep_" + outputs[out]->getName(),
513517 NTRet);
514518 new StoreInst(outputs[out], GEP, NTRet);
449449 std::vector Idx;
450450 Idx.push_back(Constant::getNullValue(Type::Int32Ty));
451451 Idx.push_back(ConstantInt::get(Type::Int32Ty, 1));
452 OldJmpBufPtr = new GetElementPtrInst(JmpBuf, &Idx[0], 2, "OldBuf",
453 EntryBB->getTerminator());
452 OldJmpBufPtr = new GetElementPtrInst(JmpBuf, Idx.begin(), Idx.end(),
453 "OldBuf", EntryBB->getTerminator());
454454
455455 // Copy the JBListHead to the alloca.
456456 Value *OldBuf = new LoadInst(JBListHead, "oldjmpbufptr", true,
488488 "setjmp.cont");
489489
490490 Idx[1] = ConstantInt::get(Type::Int32Ty, 0);
491 Value *JmpBufPtr = new GetElementPtrInst(JmpBuf, &Idx[0], Idx.size(),
491 Value *JmpBufPtr = new GetElementPtrInst(JmpBuf, Idx.begin(), Idx.end(),
492492 "TheJmpBuf",
493493 EntryBB->getTerminator());
494494 Value *SJRet = new CallInst(SetJmpFn, JmpBufPtr, "sjret",
539539 std::vector Idx;
540540 Idx.push_back(Constant::getNullValue(Type::Int32Ty));
541541 Idx.push_back(ConstantInt::get(Type::Int32Ty, 0));
542 Idx[0] = new GetElementPtrInst(BufPtr, &Idx[0], 2, "JmpBuf", UnwindBlock);
542 Idx[0] = new GetElementPtrInst(BufPtr, Idx.begin(), Idx.end(), "JmpBuf",
543 UnwindBlock);
543544 Idx[1] = ConstantInt::get(Type::Int32Ty, 1);
544545 new CallInst(LongJmpFn, Idx.begin(), Idx.end(), "", UnwindBlock);
545546 new UnreachableInst(UnwindBlock);
13491349 }
13501350
13511351 Constant *llvm::ConstantFoldGetElementPtr(const Constant *C,
1352 Constant* const *Idxs,
1352 Constant* const *Idxs,
13531353 unsigned NumIdx) {
13541354 if (NumIdx == 0 ||
13551355 (NumIdx == 1 && Idxs[0]->isNullValue()))
13571357
13581358 if (isa(C)) {
13591359 const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(),
1360 (Value**)Idxs, NumIdx,
1360 (Value **)Idxs,
1361 (Value **)Idxs+NumIdx,
13611362 true);
13621363 assert(Ty != 0 && "Invalid indices for GEP!");
13631364 return UndefValue::get(PointerType::get(Ty));
13731374 }
13741375 if (isNull) {
13751376 const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(),
1376 (Value**)Idxs, NumIdx,
1377 (Value**)Idxs,
1378 (Value**)Idxs+NumIdx,
13771379 true);
13781380 assert(Ty != 0 && "Invalid indices for GEP!");
13791381 return ConstantPointerNull::get(PointerType::get(Ty));
1717
1818 #ifndef CONSTANTFOLDING_H
1919 #define CONSTANTFOLDING_H
20
21 #include
2022
2123 namespace llvm {
2224 class Value;
4547 Constant *ConstantFoldCompareInstruction(unsigned short predicate,
4648 const Constant *C1,
4749 const Constant *C2);
50
4851 Constant *ConstantFoldGetElementPtr(const Constant *C,
4952 Constant* const *Idxs, unsigned NumIdx);
5053 } // End llvm namespace
17981798 Constant *ConstantExpr::getGetElementPtrTy(const Type *ReqTy, Constant *C,
17991799 Value* const *Idxs,
18001800 unsigned NumIdx) {
1801 assert(GetElementPtrInst::getIndexedType(C->getType(), Idxs, NumIdx, true) &&
1801 assert(GetElementPtrInst::getIndexedType(C->getType(), Idxs, Idxs+NumIdx, true) &&
18021802 "GEP indices invalid!");
18031803
18041804 if (Constant *FC = ConstantFoldGetElementPtr(C, (Constant**)Idxs, NumIdx))
18201820 unsigned NumIdx) {
18211821 // Get the result type of the getelementptr!
18221822 const Type *Ty =
1823 GetElementPtrInst::getIndexedType(C->getType(), Idxs, NumIdx, true);
1823 GetElementPtrInst::getIndexedType(C->getType(), Idxs, Idxs+NumIdx, true);
18241824 assert(Ty && "GEP indices invalid!");
18251825 return getGetElementPtrTy(PointerType::get(Ty), C, Idxs, NumIdx);
18261826 }
863863 // GetElementPtrInst Implementation
864864 //===----------------------------------------------------------------------===//
865865
866 // checkType - Simple wrapper function to give a better assertion failure
867 // message on bad indexes for a gep instruction.
868 //
869 static inline const Type *checkType(const Type *Ty) {
870 assert(Ty && "Invalid GetElementPtrInst indices for type!");
871 return Ty;
872 }
873
874866 void GetElementPtrInst::init(Value *Ptr, Value* const *Idx, unsigned NumIdx) {
875867 NumOperands = 1+NumIdx;
876868 Use *OL = OperandList = new Use[NumOperands];
878870
879871 for (unsigned i = 0; i != NumIdx; ++i)
880872 OL[i+1].init(Idx[i], this);
881 }
882
883 void GetElementPtrInst::init(Value *Ptr, Value *Idx0, Value *Idx1) {
884 NumOperands = 3;
885 Use *OL = OperandList = new Use[3];
886 OL[0].init(Ptr, this);
887 OL[1].init(Idx0, this);
888 OL[2].init(Idx1, this);
889873 }
890874
891875 void GetElementPtrInst::init(Value *Ptr, Value *Idx) {
895879 OL[1].init(Idx, this);
896880 }
897881
898
899 GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value* const *Idx,
900 unsigned NumIdx,
901 const std::string &Name, Instruction *InBe)
902 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
903 Idx, NumIdx, true))),
904 GetElementPtr, 0, 0, InBe) {
905 init(Ptr, Idx, NumIdx);
906 setName(Name);
907 }
908
909 GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value* const *Idx,
910 unsigned NumIdx,
911 const std::string &Name, BasicBlock *IAE)
912 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
913 Idx, NumIdx, true))),
914 GetElementPtr, 0, 0, IAE) {
915 init(Ptr, Idx, NumIdx);
916 setName(Name);
917 }
918
919882 GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
920883 const std::string &Name, Instruction *InBe)
921884 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx))),
929892 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx))),
930893 GetElementPtr, 0, 0, IAE) {
931894 init(Ptr, Idx);
932 setName(Name);
933 }
934
935 GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx0, Value *Idx1,
936 const std::string &Name, Instruction *InBe)
937 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
938 Idx0, Idx1, true))),
939 GetElementPtr, 0, 0, InBe) {
940 init(Ptr, Idx0, Idx1);
941 setName(Name);
942 }
943
944 GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx0, Value *Idx1,
945 const std::string &Name, BasicBlock *IAE)
946 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
947 Idx0, Idx1, true))),
948 GetElementPtr, 0, 0, IAE) {
949 init(Ptr, Idx0, Idx1);
950895 setName(Name);
951896 }
952897
996941 }
997942 }
998943 return CurIdx == NumIdx ? Ptr : 0;
999 }
1000
1001 const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
1002 Value *Idx0, Value *Idx1,
1003 bool AllowCompositeLeaf) {
1004 const PointerType *PTy = dyn_cast(Ptr);
1005 if (!PTy) return 0; // Type isn't a pointer type!
1006
1007 // Check the pointer index.
1008 if (!PTy->indexValid(Idx0)) return 0;
1009
1010 const CompositeType *CT = dyn_cast(PTy->getElementType());
1011 if (!CT || !CT->indexValid(Idx1)) return 0;
1012
1013 const Type *ElTy = CT->getTypeAtIndex(Idx1);
1014 if (AllowCompositeLeaf || ElTy->isFirstClassType())
1015 return ElTy;
1016 return 0;
1017944 }
1018945
1019946 const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) {
912912 SmallVector Idxs(GEP.idx_begin(), GEP.idx_end());
913913 const Type *ElTy =
914914 GetElementPtrInst::getIndexedType(GEP.getOperand(0)->getType(),
915 &Idxs[0], Idxs.size(), true);
915 Idxs.begin(), Idxs.end(), true);
916916 Assert1(ElTy, "Invalid indices for GEP pointer type!", &GEP);
917917 Assert2(isa(GEP.getType()) &&
918918 cast(GEP.getType())->getElementType() == ElTy,
15321532 }
15331533 }
15341534 Result.push_back(Index);
1535 Ty = GetElementPtrInst::getIndexedType(PTy, (Value**)&Result[0],
1536 Result.size(),true);
1535 Ty = GetElementPtrInst::getIndexedType(PTy, Result.begin(),
1536 Result.end(),true);
15371537 if (!Ty)
15381538 error("Index list invalid for constant getelementptr");
15391539 }
15781578 }
15791579 }
15801580 Result.push_back(Index);
1581 Ty = GetElementPtrInst::getIndexedType(PTy, &Result[0], Result.size(),true);
1581 Ty = GetElementPtrInst::getIndexedType(PTy, Result.begin(),
1582 Result.end(),true);
15821583 if (!Ty)
15831584 error("Index list invalid for constant getelementptr");
15841585 }
38893890 upgradeGEPInstIndices(Ty, $4, VIndices);
38903891
38913892 Value* tmpVal = getVal(Ty, $3);
3892 $$.I = new GetElementPtrInst(tmpVal, &VIndices[0], VIndices.size());
3893 $$.I = new GetElementPtrInst(tmpVal, VIndices.begin(), VIndices.end());
38933894 ValueInfo VI; VI.V = tmpVal; VI.S.copy($2.S);
38943895 $$.S.copy(getElementSign(VI, VIndices));
38953896 delete $2.PAT;
786786 Out << "Constant* " << constName
787787 << " = ConstantExpr::getGetElementPtr("
788788 << getCppName(CE->getOperand(0)) << ", "
789 << "&" << constName << "_indices[0], " << CE->getNumOperands() - 1
789 << constName << "_indices.begin(), "
790 << constName << "_indices.end(), "
790791 << " );";
791792 } else if (CE->isCast()) {
792793 printConstant(CE->getOperand(0));
12521253 nl(Out);
12531254 }
12541255 Out << "Instruction* " << iName << " = new GetElementPtrInst("
1255 << opNames[0] << ", &" << iName << "_indices[0], "
1256 << gep->getNumOperands() - 1;
1256 << opNames[0] << ", " << iName << "_indices.begin(), "
1257 << iName << "_indices.end()";
12571258 }
12581259 Out << ", \"";
12591260 printEscapedString(gep->getName());