llvm.org GIT mirror llvm / a7235ea
Move a few more APIs back to 2.5 forms. The only remaining ones left to change back are metadata related, which I'm waiting on to avoid conflicting with Devang. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@77721 91177308-0d34-0410-b5e6-96231b3b80d8 Owen Anderson 10 years ago
56 changed file(s) with 350 addition(s) and 399 deletion(s). Raw diff Collapse all Expand all
855855 NamedValues.erase(VarName);
856856
857857 // for expr always returns 0.0.
858 return TheFunction->getContext().getNullValue(Type::DoubleTy);
858 return Constant::getNullValue(Type::DoubleTy);
859859 }
860860
861861
15691569
15701570
15711571 // for expr always returns 0.0.
1572 return TheFunction->getContext().getNullValue(Type::DoubleTy);
1572 return Constant::getNullValue(Type::DoubleTy);
15731573 }
15741574
15751575 Function *PrototypeAST::Codegen() {
18571857
18581858
18591859 // for expr always returns 0.0.
1860 return TheFunction->getContext().getNullValue(Type::DoubleTy);
1860 return Constant::getNullValue(Type::DoubleTy);
18611861 }
18621862
18631863 Value *VarExprAST::Codegen() {
144144
145145 //call i32 @puts(i8 *getelementptr([%d x i8] *@aberrormsg, i32 0, i32 0))
146146 {
147 Constant *zero_32 = C.getNullValue(IntegerType::Int32Ty);
147 Constant *zero_32 = Constant::getNullValue(IntegerType::Int32Ty);
148148
149149 Constant *gep_params[] = {
150150 zero_32,
855855
856856
857857 // for expr always returns 0.0.
858 return TheFunction->getContext().getNullValue(Type::DoubleTy);
858 return Constant::getNullValue(Type::DoubleTy);
859859 }
860860
861861 Value *VarExprAST::Codegen() {
134134 "implemented for all constants that have operands!");
135135 assert(0 && "Constants that do not have operands cannot be using 'From'!");
136136 }
137
138 static Constant* getNullValue(const Type* Ty);
139
140 /// @returns the value for an integer constant of the given type that has all
141 /// its bits set to true.
142 /// @brief Get the all ones value
143 static Constant* getAllOnesValue(const Type* Ty);
137144 };
138145
139146 } // End llvm namespace
6464 LLVMContext();
6565 ~LLVMContext();
6666
67 // Constant accessors
68 Constant* getNullValue(const Type* Ty);
69
70 /// @returns the value for an integer constant of the given type that has all
71 /// its bits set to true.
72 /// @brief Get the all ones value
73 Constant* getAllOnesValue(const Type* Ty);
74
7567 // MDNode accessors
7668 MDNode* getMDNode(Value* const* Vals, unsigned NumVals);
7769
704704
705705 /// CreateIsNull - Return an i1 value testing if \arg Arg is null.
706706 Value *CreateIsNull(Value *Arg, const char *Name = "") {
707 return CreateICmpEQ(Arg, Context.getNullValue(Arg->getType()),
707 return CreateICmpEQ(Arg, Constant::getNullValue(Arg->getType()),
708708 Name);
709709 }
710710
711711 /// CreateIsNotNull - Return an i1 value testing if \arg Arg is not null.
712712 Value *CreateIsNotNull(Value *Arg, const char *Name = "") {
713 return CreateICmpNE(Arg, Context.getNullValue(Arg->getType()),
713 return CreateICmpNE(Arg, Constant::getNullValue(Arg->getType()),
714714 Name);
715715 }
716716
307307 AliasAnalysis::AliasResult
308308 BasicAliasAnalysis::alias(const Value *V1, unsigned V1Size,
309309 const Value *V2, unsigned V2Size) {
310 LLVMContext &Context = V1->getType()->getContext();
311
312310 // Strip off any constant expression casts if they exist
313311 if (const ConstantExpr *CE = dyn_cast(V1))
314312 if (CE->isCast() && isa(CE->getOperand(0)->getType()))
393391 // the base pointers.
394392 while (isGEP(GEP1->getOperand(0)) &&
395393 GEP1->getOperand(1) ==
396 Context.getNullValue(GEP1->getOperand(1)->getType()))
394 Constant::getNullValue(GEP1->getOperand(1)->getType()))
397395 GEP1 = cast(GEP1->getOperand(0));
398396 const Value *BasePtr1 = GEP1->getOperand(0);
399397
400398 while (isGEP(GEP2->getOperand(0)) &&
401399 GEP2->getOperand(1) ==
402 Context.getNullValue(GEP2->getOperand(1)->getType()))
400 Constant::getNullValue(GEP2->getOperand(1)->getType()))
403401 GEP2 = cast(GEP2->getOperand(0));
404402 const Value *BasePtr2 = GEP2->getOperand(0);
405403
479477 for (unsigned i = 0; i != GEPOperands.size(); ++i)
480478 if (!isa(GEPOperands[i]))
481479 GEPOperands[i] =
482 Context.getNullValue(GEPOperands[i]->getType());
480 Constant::getNullValue(GEPOperands[i]->getType());
483481 int64_t Offset =
484482 TD->getIndexedOffset(BasePtr->getType(),
485483 &GEPOperands[0],
695693 // TargetData::getIndexedOffset.
696694 for (i = 0; i != MaxOperands; ++i)
697695 if (!isa(GEP1Ops[i]))
698 GEP1Ops[i] = Context.getNullValue(GEP1Ops[i]->getType());
696 GEP1Ops[i] = Constant::getNullValue(GEP1Ops[i]->getType());
699697 // Okay, now get the offset. This is the relative offset for the full
700698 // instruction.
701699 int64_t Offset1 = TD->getIndexedOffset(GEPPointerTy, GEP1Ops,
739737 const Type *ZeroIdxTy = GEPPointerTy;
740738 for (unsigned i = 0; i != FirstConstantOper; ++i) {
741739 if (!isa(ZeroIdxTy))
742 GEP1Ops[i] = GEP2Ops[i] = Context.getNullValue(Type::Int32Ty);
740 GEP1Ops[i] = GEP2Ops[i] = Constant::getNullValue(Type::Int32Ty);
743741
744742 if (const CompositeType *CT = dyn_cast(ZeroIdxTy))
745743 ZeroIdxTy = CT->getTypeAtIndex(GEP1Ops[i]);
754752 // If they are equal, use a zero index...
755753 if (Op1 == Op2 && BasePtr1Ty == BasePtr2Ty) {
756754 if (!isa(Op1))
757 GEP1Ops[i] = GEP2Ops[i] = Context.getNullValue(Op1->getType());
755 GEP1Ops[i] = GEP2Ops[i] = Constant::getNullValue(Op1->getType());
758756 // Otherwise, just keep the constants we have.
759757 } else {
760758 if (Op1) {
799797 return MayAlias; // Be conservative with out-of-range accesses
800798 }
801799 } else { // Conservatively assume the minimum value for this index
802 GEP2Ops[i] = Context.getNullValue(Op2->getType());
800 GEP2Ops[i] = Constant::getNullValue(Op2->getType());
803801 }
804802 }
805803 }
214214 SmallVector Result;
215215 if (NumDstElt < NumSrcElt) {
216216 // Handle: bitcast (<4 x i32> to <2 x i64>)
217 Constant *Zero = Context.getNullValue(DstEltTy);
217 Constant *Zero = Constant::getNullValue(DstEltTy);
218218 unsigned Ratio = NumSrcElt/NumDstElt;
219219 unsigned SrcBitSize = SrcEltTy->getPrimitiveSizeInBits();
220220 unsigned SrcElt = 0;
418418 if (ElemIdx.ult(APInt(ElemIdx.getBitWidth(),
419419 AT->getNumElements()))) {
420420 Constant *Index[] = {
421 Context.getNullValue(CE->getType()),
421 Constant::getNullValue(CE->getType()),
422422 ConstantInt::get(Context, ElemIdx)
423423 };
424424 return
485485 // proper extension or truncation.
486486 Constant *C = ConstantExpr::getIntegerCast(CE0->getOperand(0),
487487 IntPtrTy, false);
488 Constant *NewOps[] = { C, Context.getNullValue(C->getType()) };
488 Constant *NewOps[] = { C, Constant::getNullValue(C->getType()) };
489489 return ConstantFoldCompareInstOperands(Predicate, NewOps, 2,
490490 Context, TD);
491491 }
495495 if (CE0->getOpcode() == Instruction::PtrToInt &&
496496 CE0->getType() == IntPtrTy) {
497497 Constant *C = CE0->getOperand(0);
498 Constant *NewOps[] = { C, Context.getNullValue(C->getType()) };
498 Constant *NewOps[] = { C, Constant::getNullValue(C->getType()) };
499499 // FIXME!
500500 return ConstantFoldCompareInstOperands(Predicate, NewOps, 2,
501501 Context, TD);
542542 Constant *llvm::ConstantFoldLoadThroughGEPConstantExpr(Constant *C,
543543 ConstantExpr *CE,
544544 LLVMContext &Context) {
545 if (CE->getOperand(1) != Context.getNullValue(CE->getOperand(1)->getType()))
545 if (CE->getOperand(1) != Constant::getNullValue(CE->getOperand(1)->getType()))
546546 return 0; // Do not allow stepping over the value!
547547
548548 // Loop over all of the operands, tracking down which value we are
557557 if (ConstantStruct *CS = dyn_cast(C)) {
558558 C = CS->getOperand(El);
559559 } else if (isa(C)) {
560 C = Context.getNullValue(STy->getElementType(El));
560 C = Constant::getNullValue(STy->getElementType(El));
561561 } else if (isa(C)) {
562562 C = UndefValue::get(STy->getElementType(El));
563563 } else {
570570 if (ConstantArray *CA = dyn_cast(C))
571571 C = CA->getOperand(CI->getZExtValue());
572572 else if (isa(C))
573 C = Context.getNullValue(ATy->getElementType());
573 C = Constant::getNullValue(ATy->getElementType());
574574 else if (isa(C))
575575 C = UndefValue::get(ATy->getElementType());
576576 else
581581 if (ConstantVector *CP = dyn_cast(C))
582582 C = CP->getOperand(CI->getZExtValue());
583583 else if (isa(C))
584 C = Context.getNullValue(PTy->getElementType());
584 C = Constant::getNullValue(PTy->getElementType());
585585 else if (isa(C))
586586 C = UndefValue::get(PTy->getElementType());
587587 else
740740 if (V >= -0.0)
741741 return ConstantFoldFP(sqrt, V, Ty, Context);
742742 else // Undefined
743 return Context.getNullValue(Ty);
743 return Constant::getNullValue(Ty);
744744 }
745745 break;
746746 case 's':
475475 /// getCastToEmpty - Return this descriptor as a Constant* with type '{}*'.
476476 /// This is only valid when the descriptor is non-null.
477477 Constant *DIFactory::getCastToEmpty(DIDescriptor D) {
478 if (D.isNull()) return VMContext.getNullValue(EmptyStructPtr);
478 if (D.isNull()) return llvm::Constant::getNullValue(EmptyStructPtr);
479479 return ConstantExpr::getBitCast(D.getGV(), EmptyStructPtr);
480480 }
481481
575575 unsigned RunTimeVer) {
576576 Constant *Elts[] = {
577577 GetTagConstant(dwarf::DW_TAG_compile_unit),
578 VMContext.getNullValue(EmptyStructPtr),
578 llvm::Constant::getNullValue(EmptyStructPtr),
579579 ConstantInt::get(Type::Int32Ty, LangID),
580580 GetStringConstant(Filename),
581581 GetStringConstant(Directory),
736736
737737 Constant *Elts[] = {
738738 GetTagConstant(dwarf::DW_TAG_subprogram),
739 VMContext.getNullValue(EmptyStructPtr),
739 llvm::Constant::getNullValue(EmptyStructPtr),
740740 getCastToEmpty(Context),
741741 GetStringConstant(Name),
742742 GetStringConstant(DisplayName),
768768 bool isDefinition, llvm::GlobalVariable *Val) {
769769 Constant *Elts[] = {
770770 GetTagConstant(dwarf::DW_TAG_variable),
771 VMContext.getNullValue(EmptyStructPtr),
771 llvm::Constant::getNullValue(EmptyStructPtr),
772772 getCastToEmpty(Context),
773773 GetStringConstant(Name),
774774 GetStringConstant(DisplayName),
692692 // If the object in the points-to set is the null object, then the null
693693 // pointer is a must alias.
694694 if (Pointee == &GraphNodes[NullObject])
695 RetVals.push_back(P->getContext().getNullValue(P->getType()));
695 RetVals.push_back(Constant::getNullValue(P->getType()));
696696 }
697697 }
698698 AliasAnalysis::getMustAliases(P, RetVals);
20992099 const Type *Ty = V->getType();
21002100 Ty = getEffectiveSCEVType(Ty);
21012101 return getMulExpr(V,
2102 getConstant(cast(getContext().getAllOnesValue(Ty))));
2102 getConstant(cast(Constant::getAllOnesValue(Ty))));
21032103 }
21042104
21052105 /// getNotSCEV - Return a SCEV corresponding to ~V = -1-V
21112111 const Type *Ty = V->getType();
21122112 Ty = getEffectiveSCEVType(Ty);
21132113 const SCEV *AllOnes =
2114 getConstant(cast(getContext().getAllOnesValue(Ty)));
2114 getConstant(cast(Constant::getAllOnesValue(Ty)));
21152115 return getMinusSCEV(AllOnes, V);
21162116 }
21172117
34783478 } else if (isa(Init)) {
34793479 if (const StructType *STy = dyn_cast(Init->getType())) {
34803480 assert(Idx < STy->getNumElements() && "Bad struct index!");
3481 Init = Context.getNullValue(STy->getElementType(Idx));
3481 Init = Constant::getNullValue(STy->getElementType(Idx));
34823482 } else if (const ArrayType *ATy = dyn_cast(Init->getType())) {
34833483 if (Idx >= ATy->getNumElements()) return 0; // Bogus program
3484 Init = Context.getNullValue(ATy->getElementType());
3484 Init = Constant::getNullValue(ATy->getElementType());
34853485 } else {
34863486 llvm_unreachable("Unknown constant aggregate type!");
34873487 }
284284 Ops = NewOps;
285285 AnyNonZeroIndices |= !ScaledOps.empty();
286286 Value *Scaled = ScaledOps.empty() ?
287 getContext().getNullValue(Ty) :
287 Constant::getNullValue(Ty) :
288288 expandCodeFor(SE.getAddExpr(ScaledOps), Ty);
289289 GepIndices.push_back(Scaled);
290290
400400
401401 // -1 * ... ---> 0 - ...
402402 if (FirstOp == 1)
403 V = InsertBinop(Instruction::Sub, getContext().getNullValue(Ty), V);
403 V = InsertBinop(Instruction::Sub, Constant::getNullValue(Ty), V);
404404 return V;
405405 }
406406
522522 BasicBlock *Preheader = L->getLoopPreheader();
523523 PHINode *PN = PHINode::Create(Ty, "indvar", Header->begin());
524524 InsertedValues.insert(PN);
525 PN->addIncoming(getContext().getNullValue(Ty), Preheader);
525 PN->addIncoming(Constant::getNullValue(Ty), Preheader);
526526
527527 pred_iterator HPI = pred_begin(Header);
528528 assert(HPI != pred_end(Header) && "Loop with zero preds???");
916916 idx_begin,
917917 idx_end));
918918 else if (isa(V))
919 return Context.getNullValue(ExtractValueInst::getIndexedType(PTy,
919 return Constant::getNullValue(ExtractValueInst::getIndexedType(PTy,
920920 idx_begin,
921921 idx_end));
922922 else if (Constant *C = dyn_cast(V)) {
22102210 // FIXME: LabelTy should not be a first-class type.
22112211 if (!Ty->isFirstClassType() || Ty == Type::LabelTy)
22122212 return Error(ID.Loc, "invalid type for null constant");
2213 V = Context.getNullValue(Ty);
2213 V = Constant::getNullValue(Ty);
22142214 return false;
22152215 case ValID::t_Constant:
22162216 if (ID.ConstantVal->getType() != Ty)
897897 CurTy = TypeList[Record[0]];
898898 continue; // Skip the ValueList manipulation.
899899 case bitc::CST_CODE_NULL: // NULL
900 V = Context.getNullValue(CurTy);
900 V = Constant::getNullValue(CurTy);
901901 break;
902902 case bitc::CST_CODE_INTEGER: // INTEGER: [intval]
903903 if (!isa(CurTy) || Record.empty())
992992 std::vector Elts;
993993 for (unsigned i = 0; i != Size; ++i)
994994 Elts.push_back(ConstantInt::get(EltTy, Record[i]));
995 Elts.push_back(Context.getNullValue(EltTy));
995 Elts.push_back(Constant::getNullValue(EltTy));
996996 V = ConstantArray::get(ATy, Elts);
997997 break;
998998 }
345345 }
346346 case Intrinsic::sigsetjmp:
347347 if (CI->getType() != Type::VoidTy)
348 CI->replaceAllUsesWith(Context.getNullValue(CI->getType()));
348 CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
349349 break;
350350
351351 case Intrinsic::longjmp: {
392392 "save" : "restore") << " intrinsic.\n";
393393 Warned = true;
394394 if (Callee->getIntrinsicID() == Intrinsic::stacksave)
395 CI->replaceAllUsesWith(Context.getNullValue(CI->getType()));
395 CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
396396 break;
397397 }
398398
427427 case Intrinsic::eh_exception:
428428 case Intrinsic::eh_selector_i32:
429429 case Intrinsic::eh_selector_i64:
430 CI->replaceAllUsesWith(Context.getNullValue(CI->getType()));
430 CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
431431 break;
432432
433433 case Intrinsic::eh_typeid_for_i32:
9191 } else if (isa(V)) {
9292 // Translate this as an integer zero so that it can be
9393 // local-CSE'd with actual integer zeros.
94 Reg = getRegForValue(V->getContext().getNullValue(TD.getIntPtrType()));
94 Reg = getRegForValue(Constant::getNullValue(TD.getIntPtrType()));
9595 } else if (ConstantFP *CF = dyn_cast(V)) {
9696 Reg = FastEmit_f(VT, VT, ISD::ConstantFP, CF);
9797
480480 UpdateValueMap(I, ResultReg);
481481 } else {
482482 unsigned ResultReg =
483 getRegForValue(I->getContext().getNullValue(I->getType()));
483 getRegForValue(Constant::getNullValue(I->getType()));
484484 UpdateValueMap(I, ResultReg);
485485 }
486486 return true;
293293 // linkage!
294294 Head = new GlobalVariable(M, StackEntryPtrTy, false,
295295 GlobalValue::LinkOnceAnyLinkage,
296 M.getContext().getNullValue(StackEntryPtrTy),
296 Constant::getNullValue(StackEntryPtrTy),
297297 "llvm_gc_root_chain");
298298 } else if (Head->hasExternalLinkage() && Head->isDeclaration()) {
299 Head->setInitializer(M.getContext().getNullValue(StackEntryPtrTy));
299 Head->setInitializer(Constant::getNullValue(StackEntryPtrTy));
300300 Head->setLinkage(GlobalValue::LinkOnceAnyLinkage);
301301 }
302302
6767 BasicBlock *BB = I;
6868 DeadBlocks.push_back(BB);
6969 while (PHINode *PN = dyn_cast(BB->begin())) {
70 PN->replaceAllUsesWith(F.getContext().getNullValue(PN->getType()));
70 PN->replaceAllUsesWith(Constant::getNullValue(PN->getType()));
7171 BB->getInstList().pop_front();
7272 }
7373 for (succ_iterator SI = succ_begin(BB), E = succ_end(BB); SI != E; ++SI)
11071107 std::string *ErrorMsg) {
11081108 if (AppendingVars.empty()) return false; // Nothing to do.
11091109
1110 LLVMContext &Context = M->getContext();
1111
11121110 // Loop over the multimap of appending vars, processing any variables with the
11131111 // same name, forming a new appending global variable with both of the
11141112 // initializers merged together, then rewrite references to the old variables
11681166 Inits.push_back(I->getOperand(i));
11691167 } else {
11701168 assert(isa(G1->getInitializer()));
1171 Constant *CV = Context.getNullValue(T1->getElementType());
1169 Constant *CV = Constant::getNullValue(T1->getElementType());
11721170 for (unsigned i = 0, e = T1->getNumElements(); i != e; ++i)
11731171 Inits.push_back(CV);
11741172 }
11771175 Inits.push_back(I->getOperand(i));
11781176 } else {
11791177 assert(isa(G2->getInitializer()));
1180 Constant *CV = Context.getNullValue(T2->getElementType());
1178 Constant *CV = Constant::getNullValue(T2->getElementType());
11811179 for (unsigned i = 0, e = T2->getNumElements(); i != e; ++i)
11821180 Inits.push_back(CV);
11831181 }
12351235 Out << '{';
12361236 if (AT->getNumElements()) {
12371237 Out << ' ';
1238 Constant *CZ = CPV->getContext().getNullValue(AT->getElementType());
1238 Constant *CZ = Constant::getNullValue(AT->getElementType());
12391239 printConstant(CZ, Static);
12401240 for (unsigned i = 1, e = AT->getNumElements(); i != e; ++i) {
12411241 Out << ", ";
12601260 assert(isa(CPV) || isa(CPV));
12611261 const VectorType *VT = cast(CPV->getType());
12621262 Out << "{ ";
1263 Constant *CZ = CPV->getContext().getNullValue(VT->getElementType());
1263 Constant *CZ = Constant::getNullValue(VT->getElementType());
12641264 printConstant(CZ, Static);
12651265 for (unsigned i = 1, e = VT->getNumElements(); i != e; ++i) {
12661266 Out << ", ";
12821282 Out << '{';
12831283 if (ST->getNumElements()) {
12841284 Out << ' ';
1285 printConstant(
1286 CPV->getContext().getNullValue(ST->getElementType(0)), Static);
1285 printConstant(Constant::getNullValue(ST->getElementType(0)), Static);
12871286 for (unsigned i = 1, e = ST->getNumElements(); i != e; ++i) {
12881287 Out << ", ";
1289 printConstant(
1290 CPV->getContext().getNullValue(ST->getElementType(i)), Static);
1288 printConstant(Constant::getNullValue(ST->getElementType(i)), Static);
12911289 }
12921290 }
12931291 Out << " }";
271271 const X86AddressMode &AM) {
272272 // Handle 'null' like i32/i64 0.
273273 if (isa(Val))
274 Val = Val->getContext().getNullValue(TD.getIntPtrType());
274 Val = Constant::getNullValue(TD.getIntPtrType());
275275
276276 // If this is a store of a simple constant, fold the constant into the store.
277277 if (ConstantInt *CI = dyn_cast(Val)) {
671671
672672 // Handle 'null' like i32/i64 0.
673673 if (isa(Op1))
674 Op1 = Op0->getContext().getNullValue(TD.getIntPtrType());
674 Op1 = Constant::getNullValue(TD.getIntPtrType());
675675
676676 // We have two options: compare with register or immediate. If the RHS of
677677 // the compare is an immediate that we can fold into this compare, use
22962296 MachineConstantPool &MCP = *MF.getConstantPool();
22972297 const VectorType *Ty = VectorType::get(Type::Int32Ty, 4);
22982298 Constant *C = LoadMI->getOpcode() == X86::V_SET0 ?
2299 MF.getFunction()->getContext().getNullValue(Ty) :
2300 MF.getFunction()->getContext().getAllOnesValue(Ty);
2299 Constant::getNullValue(Ty) :
2300 Constant::getAllOnesValue(Ty);
23012301 unsigned CPI = MCP.getConstantPoolIndex(C, 16);
23022302
23032303 // Create operands to load from the constant pool entry.
575575 AttributesVec.push_back(AttributeWithIndex::get(~0, attrs));
576576
577577 const Type *RetTy = FTy->getReturnType();
578 LLVMContext &Context = RetTy->getContext();
579578
580579 // Work around LLVM bug PR56: the CWriter cannot emit varargs functions which
581580 // have zero fixed arguments.
680679 }
681680
682681 if (ExtraArgHack)
683 Args.push_back(Context.getNullValue(Type::Int32Ty));
682 Args.push_back(Constant::getNullValue(Type::Int32Ty));
684683
685684 // Push any varargs arguments on the list
686685 for (; AI != CS.arg_end(); ++AI, ++ArgIndex) {
844843
845844 // Notify the alias analysis implementation that we inserted a new argument.
846845 if (ExtraArgHack)
847 AA.copyValue(Context.getNullValue(Type::Int32Ty), NF->arg_begin());
846 AA.copyValue(Constant::getNullValue(Type::Int32Ty), NF->arg_begin());
848847
849848
850849 // Tell the alias analysis that the old function is about to disappear.
600600 const Type *NRetTy = NULL;
601601 unsigned RetCount = NumRetVals(F);
602602
603 LLVMContext &Context = RetTy->getContext();
604
605603 // -1 means unused, other numbers are the new index
606604 SmallVector NewRetIdxs(RetCount, -1);
607605 std::vector RetTypes;
796794 } else if (New->getType() == Type::VoidTy) {
797795 // Our return value has uses, but they will get removed later on.
798796 // Replace by null for now.
799 Call->replaceAllUsesWith(Context.getNullValue(Call->getType()));
797 Call->replaceAllUsesWith(Constant::getNullValue(Call->getType()));
800798 } else {
801799 assert(isa(RetTy) &&
802800 "Return type changed, but not into a void. The old return type"
859857 } else {
860858 // If this argument is dead, replace any uses of it with null constants
861859 // (these are guaranteed to become unused later on).
862 I->replaceAllUsesWith(Context.getNullValue(I->getType()));
860 I->replaceAllUsesWith(Constant::getNullValue(I->getType()));
863861 }
864862
865863 // If we change the return value of the function we must rewrite any return
260260 } else if (isa(Agg)) {
261261 if (const StructType *STy = dyn_cast(Agg->getType())) {
262262 if (IdxV < STy->getNumElements())
263 return Context.getNullValue(STy->getElementType(IdxV));
263 return Constant::getNullValue(STy->getElementType(IdxV));
264264 } else if (const SequentialType *STy =
265265 dyn_cast(Agg->getType())) {
266 return Context.getNullValue(STy->getElementType());
266 return Constant::getNullValue(STy->getElementType());
267267 }
268268 } else if (isa(Agg)) {
269269 if (const StructType *STy = dyn_cast(Agg->getType())) {
549549
550550 DOUT << "PERFORMING GLOBAL SRA ON: " << *GV;
551551
552 Constant *NullInt = Context.getNullValue(Type::Int32Ty);
552 Constant *NullInt = Constant::getNullValue(Type::Int32Ty);
553553
554554 // Loop over all of the uses of the global, replacing the constantexpr geps,
555555 // with smaller constantexpr geps or direct references.
827827 Type *NewTy = ArrayType::get(MI->getAllocatedType(),
828828 NElements->getZExtValue());
829829 MallocInst *NewMI =
830 new MallocInst(NewTy, Context.getNullValue(Type::Int32Ty),
830 new MallocInst(NewTy, Constant::getNullValue(Type::Int32Ty),
831831 MI->getAlignment(), MI->getName(), MI);
832832 Value* Indices[2];
833 Indices[0] = Indices[1] = Context.getNullValue(Type::Int32Ty);
833 Indices[0] = Indices[1] = Constant::getNullValue(Type::Int32Ty);
834834 Value *NewGEP = GetElementPtrInst::Create(NewMI, Indices, Indices + 2,
835835 NewMI->getName()+".el0", MI);
836836 MI->replaceAllUsesWith(NewGEP);
11861186 Context);
11871187
11881188 Value *New = new ICmpInst(SCI, SCI->getPredicate(), NPtr,
1189 Context.getNullValue(NPtr->getType()),
1189 Constant::getNullValue(NPtr->getType()),
11901190 SCI->getName());
11911191 SCI->replaceAllUsesWith(New);
11921192 SCI->eraseFromParent();
12851285 GlobalVariable *NGV =
12861286 new GlobalVariable(*GV->getParent(),
12871287 PFieldTy, false, GlobalValue::InternalLinkage,
1288 Context.getNullValue(PFieldTy),
1288 Constant::getNullValue(PFieldTy),
12891289 GV->getName() + ".f" + Twine(FieldNo), GV,
12901290 GV->isThreadLocal());
12911291 FieldGlobals.push_back(NGV);
13111311 Value *RunningOr = 0;
13121312 for (unsigned i = 0, e = FieldMallocs.size(); i != e; ++i) {
13131313 Value *Cond = new ICmpInst(MI, ICmpInst::ICMP_EQ, FieldMallocs[i],
1314 Context.getNullValue(FieldMallocs[i]->getType()),
1314 Constant::getNullValue(FieldMallocs[i]->getType()),
13151315 "isnull");
13161316 if (!RunningOr)
13171317 RunningOr = Cond; // First seteq
13381338 for (unsigned i = 0, e = FieldGlobals.size(); i != e; ++i) {
13391339 Value *GVVal = new LoadInst(FieldGlobals[i], "tmp", NullPtrBlock);
13401340 Value *Cmp = new ICmpInst(*NullPtrBlock, ICmpInst::ICMP_NE, GVVal,
1341 Context.getNullValue(GVVal->getType()),
1341 Constant::getNullValue(GVVal->getType()),
13421342 "tmp");
13431343 BasicBlock *FreeBlock = BasicBlock::Create("free_it", OrigBB->getParent());
13441344 BasicBlock *NextBlock = BasicBlock::Create("next", OrigBB->getParent());
13461346
13471347 // Fill in FreeBlock.
13481348 new FreeInst(GVVal, FreeBlock);
1349 new StoreInst(Context.getNullValue(GVVal->getType()), FieldGlobals[i],
1349 new StoreInst(Constant::getNullValue(GVVal->getType()), FieldGlobals[i],
13501350 FreeBlock);
13511351 BranchInst::Create(NextBlock, FreeBlock);
13521352
13861386 // Insert a store of null into each global.
13871387 for (unsigned i = 0, e = FieldGlobals.size(); i != e; ++i) {
13881388 const PointerType *PT = cast(FieldGlobals[i]->getType());
1389 Constant *Null = Context.getNullValue(PT->getElementType());
1389 Constant *Null = Constant::getNullValue(PT->getElementType());
13901390 new StoreInst(Null, FieldGlobals[i], SI);
13911391 }
13921392 // Erase the original store.
19571957 } else {
19581958 const Type *FTy = FunctionType::get(Type::VoidTy, false);
19591959 const PointerType *PFTy = PointerType::getUnqual(FTy);
1960 CSVals[1] = Context.getNullValue(PFTy);
1960 CSVals[1] = Constant::getNullValue(PFTy);
19611961 CSVals[0] = ConstantInt::get(Type::Int32Ty, 2147483647);
19621962 }
19631963 CAList.push_back(ConstantStruct::get(CSVals));
20522052 Elts.push_back(cast(*i));
20532053 } else if (isa(Init)) {
20542054 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
2055 Elts.push_back(Context.getNullValue(STy->getElementType(i)));
2055 Elts.push_back(Constant::getNullValue(STy->getElementType(i)));
20562056 } else if (isa(Init)) {
20572057 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
20582058 Elts.push_back(UndefValue::get(STy->getElementType(i)));
20792079 for (User::op_iterator i = CA->op_begin(), e = CA->op_end(); i != e; ++i)
20802080 Elts.push_back(cast(*i));
20812081 } else if (isa(Init)) {
2082 Constant *Elt = Context.getNullValue(ATy->getElementType());
2082 Constant *Elt = Constant::getNullValue(ATy->getElementType());
20832083 Elts.assign(ATy->getNumElements(), Elt);
20842084 } else if (isa(Init)) {
20852085 Constant *Elt = UndefValue::get(ATy->getElementType());
23682368 // silly, e.g. storing the address of the alloca somewhere and using it
23692369 // later. Since this is undefined, we'll just make it be null.
23702370 if (!Tmp->use_empty())
2371 Tmp->replaceAllUsesWith(F->getContext().getNullValue(Tmp->getType()));
2371 Tmp->replaceAllUsesWith(Constant::getNullValue(Tmp->getType()));
23722372 delete Tmp;
23732373 }
23742374
426426 PHINode* PHI = PHINode::Create(Type::Int32Ty, "SetJmpReturn", Inst);
427427
428428 // Coming from a call to setjmp, the return is 0.
429 PHI->addIncoming(Inst->getContext().getNullValue(Type::Int32Ty), ABlock);
429 PHI->addIncoming(Constant::getNullValue(Type::Int32Ty), ABlock);
430430
431431 // Add the case for this setjmp's number...
432432 SwitchValuePair SVP = GetSJSwitch(Func, GetRethrowBB(Func));
6565 const Type *ATy = ArrayType::get(Type::Int32Ty, NumFunctions);
6666 GlobalVariable *Counters =
6767 new GlobalVariable(M, ATy, false, GlobalValue::InternalLinkage,
68 M.getContext().getNullValue(ATy), "FuncProfCounters");
68 Constant::getNullValue(ATy), "FuncProfCounters");
6969
7070 // Instrument all of the functions...
7171 unsigned i = 0;
110110 const Type *ATy = ArrayType::get(Type::Int32Ty, NumBlocks);
111111 GlobalVariable *Counters =
112112 new GlobalVariable(M, ATy, false, GlobalValue::InternalLinkage,
113 M.getContext().getNullValue(ATy), "BlockProfCounters");
113 Constant::getNullValue(ATy), "BlockProfCounters");
114114
115115 // Instrument all of the blocks...
116116 unsigned i = 0;
6666 const Type *ATy = ArrayType::get(Type::Int32Ty, NumEdges);
6767 GlobalVariable *Counters =
6868 new GlobalVariable(M, ATy, false, GlobalValue::InternalLinkage,
69 M.getContext().getNullValue(ATy), "EdgeProfCounters");
69 Constant::getNullValue(ATy), "EdgeProfCounters");
7070
7171 // Instrument all of the edges...
7272 unsigned i = 0;
2222
2323 void llvm::InsertProfilingInitCall(Function *MainFn, const char *FnName,
2424 GlobalValue *Array) {
25 LLVMContext &Context = MainFn->getContext();
2625 const Type *ArgVTy =
2726 PointerType::getUnqual(PointerType::getUnqual(Type::Int8Ty));
2827 const PointerType *UIntPtr = PointerType::getUnqual(Type::Int32Ty);
3433 // This could force argc and argv into programs that wouldn't otherwise have
3534 // them, but instead we just pass null values in.
3635 std::vector Args(4);
37 Args[0] = Context.getNullValue(Type::Int32Ty);
38 Args[1] = Context.getNullValue(ArgVTy);
36 Args[0] = Constant::getNullValue(Type::Int32Ty);
37 Args[1] = Constant::getNullValue(ArgVTy);
3938
4039 // Skip over any allocas in the entry block.
4140 BasicBlock *Entry = MainFn->begin();
4241 BasicBlock::iterator InsertPos = Entry->begin();
4342 while (isa(InsertPos)) ++InsertPos;
4443
45 std::vector GEPIndices(2, Context.getNullValue(Type::Int32Ty));
44 std::vector GEPIndices(2, Constant::getNullValue(Type::Int32Ty));
4645 unsigned NumElements = 0;
4746 if (Array) {
4847 Args[2] = ConstantExpr::getGetElementPtr(Array, &GEPIndices[0],
10099
101100 void llvm::IncrementCounterInBlock(BasicBlock *BB, unsigned CounterNum,
102101 GlobalValue *CounterArray) {
103 LLVMContext &Context = BB->getContext();
104
105102 // Insert the increment after any alloca or PHI instructions...
106103 BasicBlock::iterator InsertPos = BB->getFirstNonPHI();
107104 while (isa(InsertPos))
109106
110107 // Create the getelementptr constant expression
111108 std::vector Indices(2);
112 Indices[0] = Context.getNullValue(Type::Int32Ty);
109 Indices[0] = Constant::getNullValue(Type::Int32Ty);
113110 Indices[1] = ConstantInt::get(Type::Int32Ty, CounterNum);
114111 Constant *ElementPtr =
115112 ConstantExpr::getGetElementPtr(CounterArray, &Indices[0],
349349
350350 // Create the getelementptr constant expression
351351 std::vector Indices(2);
352 Indices[0] = BB->getContext().getNullValue(Type::Int32Ty);
352 Indices[0] = Constant::getNullValue(Type::Int32Ty);
353353 Indices[1] = ConstantInt::get(Type::Int32Ty, CounterNum);
354354 Constant *ElementPtr =ConstantExpr::getGetElementPtr(CounterArray,
355355 &Indices[0], 2);
559559 bool CodeGenPrepare::OptimizeMemoryInst(Instruction *MemoryInst, Value *Addr,
560560 const Type *AccessTy,
561561 DenseMap &SunkAddrs) {
562 LLVMContext &Context = MemoryInst->getContext();
563
564562 // Figure out what addressing mode will be built up for this operation.
565563 SmallVector AddrModeInsts;
566564 ExtAddrMode AddrMode = AddressingModeMatcher::Match(Addr, AccessTy,MemoryInst,
657655 }
658656
659657 if (Result == 0)
660 SunkAddr = Context.getNullValue(Addr->getType());
658 SunkAddr = Constant::getNullValue(Addr->getType());
661659 else
662660 SunkAddr = new IntToPtrInst(Result, Addr->getType(), "sunkaddr",InsertPt);
663661 }
856856
857857 // If all of the demanded bits in the inputs are known zeros, return zero.
858858 if ((DemandedMask & (RHSKnownZero|LHSKnownZero)) == DemandedMask)
859 return Context->getNullValue(VTy);
859 return Constant::getNullValue(VTy);
860860
861861 } else if (I->getOpcode() == Instruction::Or) {
862862 // We can simplify (X|Y) -> X or Y in the user's context if we know that
925925
926926 // If all of the demanded bits in the inputs are known zeros, return zero.
927927 if ((DemandedMask & (RHSKnownZero|LHSKnownZero)) == DemandedMask)
928 return Context->getNullValue(VTy);
928 return Constant::getNullValue(VTy);
929929
930930 // If the RHS is a constant, see if we can simplify it.
931931 if (ShrinkDemandedConstant(I, 1, DemandedMask & ~LHSKnownZero, Context))
14691469 return 0;
14701470
14711471 const Type *EltTy = cast(V->getType())->getElementType();
1472 Constant *Zero = Context->getNullValue(EltTy);
1472 Constant *Zero = Constant::getNullValue(EltTy);
14731473 Constant *Undef = UndefValue::get(EltTy);
14741474 std::vector Elts;
14751475 for (unsigned i = 0; i != VWidth; ++i) {
18071807 // Make what used to be the LHS of the root be the user of the root...
18081808 Value *ExtraOperand = TmpLHSI->getOperand(1);
18091809 if (&Root == TmpLHSI) {
1810 Root.replaceAllUsesWith(Context->getNullValue(TmpLHSI->getType()));
1810 Root.replaceAllUsesWith(Constant::getNullValue(TmpLHSI->getType()));
18111811 return 0;
18121812 }
18131813 Root.replaceAllUsesWith(TmpLHSI); // Users now use TmpLHSI
21862186 // X + ~X --> -1 since ~X = -X-1
21872187 if (dyn_castNotVal(LHS, Context) == RHS ||
21882188 dyn_castNotVal(RHS, Context) == LHS)
2189 return ReplaceInstUsesWith(I, Context->getAllOnesValue(I.getType()));
2189 return ReplaceInstUsesWith(I, Constant::getAllOnesValue(I.getType()));
21902190
21912191
21922192 // (A & C1)+(B & C2) --> (A & C1)|(B & C2) iff C1&C2 == 0
24162416 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
24172417
24182418 if (Op0 == Op1) // sub X, X -> 0
2419 return ReplaceInstUsesWith(I, Context->getNullValue(I.getType()));
2419 return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
24202420
24212421 // If this is a 'B = x-(-A)', change to B = x+A...
24222422 if (Value *V = dyn_castNegVal(Op1, Context))
26222622 Value *Op0 = I.getOperand(0);
26232623
26242624 if (isa(I.getOperand(1))) // undef * X -> 0
2625 return ReplaceInstUsesWith(I, Context->getNullValue(I.getType()));
2625 return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
26262626
26272627 // Simplify mul instructions with a constant RHS...
26282628 if (Constant *Op1 = dyn_cast(I.getOperand(1))) {
29032903 if (isa(Op0)) {
29042904 if (Op0->getType()->isFPOrFPVector())
29052905 return ReplaceInstUsesWith(I, Op0);
2906 return ReplaceInstUsesWith(I, Context->getNullValue(I.getType()));
2906 return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
29072907 }
29082908
29092909 // X / undef -> undef
29512951 if (ConstantInt *LHSRHS = dyn_cast(LHS->getOperand(1))) {
29522952 if (MultiplyOverflows(RHS, LHSRHS,
29532953 I.getOpcode()==Instruction::SDiv, Context))
2954 return ReplaceInstUsesWith(I, Context->getNullValue(I.getType()));
2954 return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
29552955 else
29562956 return BinaryOperator::Create(I.getOpcode(), LHS->getOperand(0),
29572957 ConstantExpr::getMul(RHS, LHSRHS));
29702970 // 0 / X == 0, we don't need to preserve faults!
29712971 if (ConstantInt *LHS = dyn_cast(Op0))
29722972 if (LHS->equalsInt(0))
2973 return ReplaceInstUsesWith(I, Context->getNullValue(I.getType()));
2973 return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
29742974
29752975 // It can't be division by zero, hence it must be division by one.
29762976 if (I.getType() == Type::Int1Ty)
30063006 Value *IC = InsertNewInstBefore(new ICmpInst(*Context,
30073007 ICmpInst::ICMP_ULT, Op0, C),
30083008 I);
3009 return SelectInst::Create(IC, Context->getNullValue(I.getType()),
3009 return SelectInst::Create(IC, Constant::getNullValue(I.getType()),
30103010 ConstantInt::get(I.getType(), 1));
30113011 }
30123012 }
31073107 if (isa(Op0)) { // undef % X -> 0
31083108 if (I.getType()->isFPOrFPVector())
31093109 return ReplaceInstUsesWith(I, Op0); // X % undef -> undef (could be SNaN)
3110 return ReplaceInstUsesWith(I, Context->getNullValue(I.getType()));
3110 return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
31113111 }
31123112 if (isa(Op1))
31133113 return ReplaceInstUsesWith(I, Op1); // X % undef -> undef
31323132 // 0 % X == 0 for integer, we don't need to preserve faults!
31333133 if (Constant *LHS = dyn_cast(Op0))
31343134 if (LHS->isNullValue())
3135 return ReplaceInstUsesWith(I, Context->getNullValue(I.getType()));
3135 return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
31363136
31373137 if (ConstantInt *RHS = dyn_cast(Op1)) {
31383138 // X % 0 == undef, we don't need to preserve faults!
31403140 return ReplaceInstUsesWith(I, UndefValue::get(I.getType()));
31413141
31423142 if (RHS->equalsInt(1)) // X % 1 == 0
3143 return ReplaceInstUsesWith(I, Context->getNullValue(I.getType()));
3143 return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
31443144
31453145 if (Instruction *Op0I = dyn_cast(Op0)) {
31463146 if (SelectInst *SI = dyn_cast(Op0I)) {
31803180 if (RHSI->getOpcode() == Instruction::Shl &&
31813181 isa(RHSI->getOperand(0))) {
31823182 if (cast(RHSI->getOperand(0))->getValue().isPowerOf2()) {
3183 Constant *N1 = Context->getAllOnesValue(I.getType());
3183 Constant *N1 = Constant::getAllOnesValue(I.getType());
31843184 Value *Add = InsertNewInstBefore(BinaryOperator::CreateAdd(RHSI, N1,
31853185 "tmp"), I);
31863186 return BinaryOperator::CreateAnd(Op0, Add);
40074007 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
40084008
40094009 if (isa(Op1)) // X & undef -> 0
4010 return ReplaceInstUsesWith(I, Context->getNullValue(I.getType()));
4010 return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
40114011
40124012 // and X, X = X
40134013 if (Op0 == Op1)
41004100 // (1 >> x) & 1 --> zext(x == 0)
41014101 if (AndRHSMask == 1 && Op0LHS == AndRHS) {
41024102 Instruction *NewICmp = new ICmpInst(*Context, ICmpInst::ICMP_EQ,
4103 Op0RHS, Context->getNullValue(I.getType()));
4103 Op0RHS, Constant::getNullValue(I.getType()));
41044104 InsertNewInstBefore(NewICmp, I);
41054105 return new ZExtInst(NewICmp, I.getType());
41064106 }
41584158 Value *Op1NotVal = dyn_castNotVal(Op1, Context);
41594159
41604160 if (Op0NotVal == Op1 || Op1NotVal == Op0) // A & ~A == ~A & A == 0
4161 return ReplaceInstUsesWith(I, Context->getNullValue(I.getType()));
4161 return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
41624162
41634163 // (~A & ~B) == (~(A | B)) - De Morgan's Law
41644164 if (Op0NotVal && Op1NotVal && isOnlyUse(Op0) && isOnlyUse(Op1)) {
47324732 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
47334733
47344734 if (isa(Op1)) // X | undef -> -1
4735 return ReplaceInstUsesWith(I, Context->getAllOnesValue(I.getType()));
4735 return ReplaceInstUsesWith(I, Constant::getAllOnesValue(I.getType()));
47364736
47374737 // or X, X = X
47384738 if (Op0 == Op1)
49334933
49344934 if (match(Op0, m_Not(m_Value(A)), *Context)) { // ~A | Op1
49354935 if (A == Op1) // ~A | A == -1
4936 return ReplaceInstUsesWith(I, Context->getAllOnesValue(I.getType()));
4936 return ReplaceInstUsesWith(I, Constant::getAllOnesValue(I.getType()));
49374937 } else {
49384938 A = 0;
49394939 }
49404940 // Note, A is still live here!
49414941 if (match(Op1, m_Not(m_Value(B)), *Context)) { // Op0 | ~B
49424942 if (Op0 == B)
4943 return ReplaceInstUsesWith(I, Context->getAllOnesValue(I.getType()));
4943 return ReplaceInstUsesWith(I, Constant::getAllOnesValue(I.getType()));
49444944
49454945 // (~A | ~B) == (~(A & B)) - De Morgan's Law
49464946 if (A && isOnlyUse(Op0) && isOnlyUse(Op1)) {
50185018 if (isa(Op0))
50195019 // Handle undef ^ undef -> 0 special case. This is a common
50205020 // idiom (misuse).
5021 return ReplaceInstUsesWith(I, Context->getNullValue(I.getType()));
5021 return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
50225022 return ReplaceInstUsesWith(I, Op1); // X ^ undef -> undef
50235023 }
50245024
50255025 // xor X, X = 0, even if X is nested in a sequence of Xor's.
50265026 if (Instruction *Result = AssociativeOpt(I, XorSelf(Op1), Context)) {
50275027 assert(Result == &I && "AssociativeOpt didn't work?"); Result=Result;
5028 return ReplaceInstUsesWith(I, Context->getNullValue(I.getType()));
5028 return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
50295029 }
50305030
50315031 // See if we can simplify any instructions used by the instruction whose sole
51475147
51485148 if (Value *X = dyn_castNotVal(Op0, Context)) // ~A ^ A == -1
51495149 if (X == Op1)
5150 return ReplaceInstUsesWith(I, Context->getAllOnesValue(I.getType()));
5150 return ReplaceInstUsesWith(I, Constant::getAllOnesValue(I.getType()));
51515151
51525152 if (Value *X = dyn_castNotVal(Op1, Context)) // A ^ ~A == -1
51535153 if (X == Op0)
5154 return ReplaceInstUsesWith(I, Context->getAllOnesValue(I.getType()));
5154 return ReplaceInstUsesWith(I, Constant::getAllOnesValue(I.getType()));
51555155
51565156
51575157 BinaryOperator *Op1I = dyn_cast(Op1);
53775377 gep_type_iterator GTI = gep_type_begin(GEP);
53785378 const Type *IntPtrTy = TD.getIntPtrType();
53795379 LLVMContext *Context = IC.getContext();
5380 Value *Result = Context->getNullValue(IntPtrTy);
5380 Value *Result = Constant::getNullValue(IntPtrTy);
53815381
53825382 // Build a mask for high order bits.
53835383 unsigned IntPtrWidth = TD.getPointerSizeInBits();
55775577 if (Offset == 0)
55785578 Offset = EmitGEPOffset(GEPLHS, I, *this);
55795579 return new ICmpInst(*Context, ICmpInst::getSignedPredicate(Cond), Offset,
5580 Context->getNullValue(Offset->getType()));
5580 Constant::getNullValue(Offset->getType()));
55815581 } else if (GEPOperator *GEPRHS = dyn_cast(RHS)) {
55825582 // If the base pointers are different, but the indices are the same, just
55835583 // compare the base pointer.
58915891 case FCmpInst::FCMP_UNE: // True if unordered or not equal
58925892 // Canonicalize these to be 'fcmp uno %X, 0.0'.
58935893 I.setPredicate(FCmpInst::FCMP_UNO);
5894 I.setOperand(1, Context->getNullValue(Op0->getType()));
5894 I.setOperand(1, Constant::getNullValue(Op0->getType()));
58955895 return &I;
58965896
58975897 case FCmpInst::FCMP_ORD: // True if ordered (no nans)
59005900 case FCmpInst::FCMP_OLE: // True if ordered and less than or equal
59015901 // Canonicalize these to be 'fcmp ord %X, 0.0'.
59025902 I.setPredicate(FCmpInst::FCMP_ORD);
5903 I.setOperand(1, Context->getNullValue(Op0->getType()));
5903 I.setOperand(1, Constant::getNullValue(Op0->getType()));
59045904 return &I;
59055905 }
59065906 }
61646164 // (x (x >s -1) -> true if sign bit clear
61656165 if (CI->isMinValue(true))
61666166 return new ICmpInst(*Context, ICmpInst::ICMP_SGT, Op0,
6167 Context->getAllOnesValue(Op0->getType()));
6167 Constant::getAllOnesValue(Op0->getType()));
61686168 }
61696169 break;
61706170 case ICmpInst::ICMP_UGT:
61836183 // (x >u 2147483647) -> (x true if sign bit set
61846184 if (CI->isMaxValue(true))
61856185 return new ICmpInst(*Context, ICmpInst::ICMP_SLT, Op0,
6186 Context->getNullValue(Op0->getType()));
6186 Constant::getNullValue(Op0->getType()));
61876187 }
61886188 break;
61896189 case ICmpInst::ICMP_SLT:
62916291 }
62926292 if (isAllZeros)
62936293 return new ICmpInst(*Context, I.getPredicate(), LHSI->getOperand(0),
6294 Context->getNullValue(LHSI->getOperand(0)->getType()));
6294 Constant::getNullValue(LHSI->getOperand(0)->getType()));
62956295 }
62966296 break;
62976297
64716471 if (A == Op1 || B == Op1) { // (A^B) == A -> B == 0
64726472 Value *OtherVal = A == Op1 ? B : A;
64736473 return new ICmpInst(*Context, I.getPredicate(), OtherVal,
6474 Context->getNullValue(A->getType()));
6474 Constant::getNullValue(A->getType()));
64756475 }
64766476
64776477 if (match(Op1, m_Xor(m_Value(C), m_Value(D)), *Context)) {
64996499 // A == (A^B) -> B == 0
65006500 Value *OtherVal = A == Op0 ? B : A;
65016501 return new ICmpInst(*Context, I.getPredicate(), OtherVal,
6502 Context->getNullValue(A->getType()));
6502 Constant::getNullValue(A->getType()));
65036503 }
65046504
65056505 // (A-B) == A -> B == 0
65066506 if (match(Op0, m_Sub(m_Specific(Op1), m_Value(B)), *Context))
65076507 return new ICmpInst(*Context, I.getPredicate(), B,
6508 Context->getNullValue(B->getType()));
6508 Constant::getNullValue(B->getType()));
65096509
65106510 // A == (A-B) -> B == 0
65116511 if (match(Op1, m_Sub(m_Specific(Op0), m_Value(B)), *Context))
65126512 return new ICmpInst(*Context, I.getPredicate(), B,
6513 Context->getNullValue(B->getType()));
6513 Constant::getNullValue(B->getType()));
65146514
65156515 // (X&Z) == (Y&Z) -> (X^Y) & Z == 0
65166516 if (Op0->hasOneUse() && Op1->hasOneUse() &&
65326532 Op1 = InsertNewInstBefore(BinaryOperator::CreateXor(X, Y, "tmp"), I);
65336533 Op1 = InsertNewInstBefore(BinaryOperator::CreateAnd(Op1, Z, "tmp"), I);
65346534 I.setOperand(0, Op1);
6535 I.setOperand(1, Context->getNullValue(Op1->getType()));
6535 I.setOperand(1, Constant::getNullValue(Op1->getType()));
65366536 return &I;
65376537 }
65386538 }
69556955
69566956 return new ICmpInst(*Context,
69576957 TrueIfSigned ? ICmpInst::ICMP_NE : ICmpInst::ICMP_EQ,
6958 And, Context->getNullValue(And->getType()));
6958 And, Constant::getNullValue(And->getType()));
69596959 }
69606960 break;
69616961 }
70787078 BO->getName());
70797079 InsertNewInstBefore(NewRem, ICI);
70807080 return new ICmpInst(*Context, ICI.getPredicate(), NewRem,
7081 Context->getNullValue(BO->getType()));
7081 Constant::getNullValue(BO->getType()));
70827082 }
70837083 }
70847084 break;
71457145 if (RHS == BOC && RHSV.isPowerOf2())
71467146 return new ICmpInst(*Context, isICMP_NE ? ICmpInst::ICMP_EQ :
71477147 ICmpInst::ICMP_NE, LHSI,
7148 Context->getNullValue(RHS->getType()));
7148 Constant::getNullValue(RHS->getType()));
71497149
71507150 // Replace (and X, (1 << size(X)-1) != 0) with x s< 0
71517151 if (BOC->getValue().isSignBit()) {
71527152 Value *X = BO->getOperand(0);
7153 Constant *Zero = Context->getNullValue(X->getType());
7153 Constant *Zero = Constant::getNullValue(X->getType());
71547154 ICmpInst::Predicate pred = isICMP_NE ?
71557155 ICmpInst::ICMP_SLT : ICmpInst::ICMP_SGE;
71567156 return new ICmpInst(*Context, pred, X, Zero);
72947294 if (isSignedExt) {
72957295 // We're performing an unsigned comp with a sign extended value.
72967296 // This is true if the input is >= 0. [aka >s -1]
7297 Constant *NegOne = Context->getAllOnesValue(SrcTy);
7297 Constant *NegOne = Constant::getAllOnesValue(SrcTy);
72987298 Result = InsertNewInstBefore(new ICmpInst(*Context, ICmpInst::ICMP_SGT,
72997299 LHSCIOp, NegOne, ICI.getName()), ICI);
73007300 } else {
73547354
73557355 // shl X, 0 == X and shr X, 0 == X
73567356 // shl 0, X == 0 and shr 0, X == 0
7357 if (Op1 == Context->getNullValue(Op1->getType()) ||
7358 Op0 == Context->getNullValue(Op0->getType()))
7357 if (Op1 == Constant::getNullValue(Op1->getType()) ||
7358 Op0 == Constant::getNullValue(Op0->getType()))
73597359 return ReplaceInstUsesWith(I, Op0);
73607360
73617361 if (isa(Op0)) {
73627362 if (I.getOpcode() == Instruction::AShr) // undef >>s X -> undef
73637363 return ReplaceInstUsesWith(I, Op0);
73647364 else // undef << X -> 0, undef >>u X -> 0
7365 return ReplaceInstUsesWith(I, Context->getNullValue(I.getType()));
7365 return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
73667366 }
73677367 if (isa(Op1)) {
73687368 if (I.getOpcode() == Instruction::AShr) // X >>s undef -> X
73697369 return ReplaceInstUsesWith(I, Op0);
73707370 else // X << undef, X >>u undef -> 0
7371 return ReplaceInstUsesWith(I, Context->getNullValue(I.getType()));
7371 return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
73727372 }
73737373
73747374 // See if we can fold away this shift.
74007400 //
74017401 if (Op1->uge(TypeBits)) {
74027402 if (I.getOpcode() != Instruction::AShr)
7403 return ReplaceInstUsesWith(I, Context->getNullValue(Op0->getType()));
7403 return ReplaceInstUsesWith(I, Constant::getNullValue(Op0->getType()));
74047404 else {
74057405 I.setOperand(1, ConstantInt::get(I.getType(), TypeBits-1));
74067406 return &I;
76287628 // saturates.
76297629 if (AmtSum >= TypeBits) {
76307630 if (I.getOpcode() != Instruction::AShr)
7631 return ReplaceInstUsesWith(I, Context->getNullValue(I.getType()));
7631 return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
76327632 AmtSum = TypeBits-1; // Saturate to 31 for i32 ashr.
76337633 }
76347634
76377637 } else if (ShiftOp->getOpcode() == Instruction::LShr &&
76387638 I.getOpcode() == Instruction::AShr) {
76397639 if (AmtSum >= TypeBits)
7640 return ReplaceInstUsesWith(I, Context->getNullValue(I.getType()));
7640 return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
76417641
76427642 // ((X >>u C1) >>s C2) -> (X >>u (C1+C2)) since C1 != 0.
76437643 return BinaryOperator::CreateLShr(X, ConstantInt::get(Ty, AmtSum));
84768476 if (DestBitWidth == 1) {
84778477 Constant *One = ConstantInt::get(Src->getType(), 1);
84788478 Src = InsertNewInstBefore(BinaryOperator::CreateAnd(Src, One, "tmp"), CI);
8479 Value *Zero = Context->getNullValue(Src->getType());
8479 Value *Zero = Constant::getNullValue(Src->getType());
84808480 return new ICmpInst(*Context, ICmpInst::ICMP_NE, Src, Zero);
84818481 }
84828482
84918491 APInt Mask(APInt::getLowBitsSet(SrcBitWidth, ShAmt).shl(DestBitWidth));
84928492 if (MaskedValueIsZero(ShiftOp, Mask)) {
84938493 if (ShAmt >= DestBitWidth) // All zeros.
8494 return ReplaceInstUsesWith(CI, Context->getNullValue(Ty));
8494 return ReplaceInstUsesWith(CI, Constant::getNullValue(Ty));
84958495
84968496 // Okay, we can shrink this. Truncate the input, then return a new
84978497 // shift.
86988698 // Canonicalize sign-extend from i1 to a select.
86998699 if (Src->getType() == Type::Int1Ty)
87008700 return SelectInst::Create(Src,
8701 Context->getAllOnesValue(CI.getType()),
8702 Context->getNullValue(CI.getType()));
8701 Constant::getAllOnesValue(CI.getType()),
8702 Constant::getNullValue(CI.getType()));
87038703
87048704 // See if the value being truncated is already sign extended. If so, just
87058705 // eliminate the trunc/sext pair.
89698969 // If the source and destination are pointers, and this cast is equivalent
89708970 // to a getelementptr X, 0, 0, 0... turn it into the appropriate gep.
89718971 // This can enhance SROA and other transforms that want type-safe pointers.
8972 Constant *ZeroUInt = Context->getNullValue(Type::Int32Ty);
8972 Constant *ZeroUInt = Constant::getNullValue(Type::Int32Ty);
89738973 unsigned NumZeros = 0;
89748974 while (SrcElTy != DstElTy &&
89758975 isa(SrcElTy) && !isa(SrcElTy) &&
89958995 Value *Elem = InsertCastBefore(Instruction::BitCast, Src,
89968996 DestVTy->getElementType(), CI);
89978997 return InsertElementInst::Create(UndefValue::get(DestTy), Elem,
8998 Context->getNullValue(Type::Int32Ty));
8998 Constant::getNullValue(Type::Int32Ty));
89998999 }
90009000 // FIXME: Canonicalize bitcast(insertelement) -> insertelement(bitcast)
90019001 }
90059005 if (SrcVTy->getNumElements() == 1) {
90069006 if (!isa(DestTy)) {
90079007 Instruction *Elem =
9008 ExtractElementInst::Create(Src, Context->getNullValue(Type::Int32Ty));
9008 ExtractElementInst::Create(Src, Constant::getNullValue(Type::Int32Ty));
90099009 InsertNewInstBefore(Elem, CI);
90109010 return CastInst::Create(Instruction::BitCast, Elem, DestTy);
90119011 }
90859085 case Instruction::Shl:
90869086 case Instruction::LShr:
90879087 case Instruction::AShr:
9088 return Context->getNullValue(I->getType());
9088 return Constant::getNullValue(I->getType());
90899089 case Instruction::And:
9090 return Context->getAllOnesValue(I->getType());
9090 return Constant::getAllOnesValue(I->getType());
90919091 case Instruction::Mul:
90929092 return ConstantInt::get(I->getType(), 1);
90939093 }
97429742 InsertNewInstBefore(new StoreInst(L, Dest, false, DstAlign), *MI);
97439743
97449744 // Set the size of the copy to 0, it will be deleted on the next iteration.
9745 MI->setOperand(3, Context->getNullValue(MemOpLength->getType()));
9745 MI->setOperand(3, Constant::getNullValue(MemOpLength->getType()));
97469746 return MI;
97479747 }
97489748
97819781 Dest, false, Alignment), *MI);
97829782
97839783 // Set the size of the copy to 0, it will be deleted on the next iteration.
9784 MI->setLength(Context->getNullValue(LenC->getType()));
9784 MI->setLength(Constant::getNullValue(LenC->getType()));
97859785 return MI;
97869786 }
97879787
1025810258 // If the function takes more arguments than the call was taking, add them
1025910259 // now...
1026010260 for (unsigned i = NumCommonArgs; i != FT->getNumParams(); ++i)
10261 Args.push_back(Context->getNullValue(FT->getParamType(i)));
10261 Args.push_back(Constant::getNullValue(FT->getParamType(i)));
1026210262
1026310263 // If we are removing arguments to the function, emit an obnoxious warning...
1026410264 if (FT->getNumParams() < NumActualArgs) {
1109111091 // With: T = long A+B; gep %P, T, ...
1109211092 //
1109311093 Value *Sum, *SO1 = SrcGEPOperands.back(), *GO1 = GEP.getOperand(1);
11094 if (SO1 == Context->getNullValue(SO1->getType())) {
11094 if (SO1 == Constant::getNullValue(SO1->getType())) {
1109511095 Sum = GO1;
11096 } else if (GO1 == Context->getNullValue(GO1->getType())) {
11096 } else if (GO1 == Constant::getNullValue(GO1->getType())) {
1109711097 Sum = SO1;
1109811098 } else {
1109911099 // If they aren't the same type, convert both to an integer of the
1122611226 TD->getTypeAllocSize(cast(SrcElTy)->getElementType()) ==
1122711227 TD->getTypeAllocSize(ResElTy)) {
1122811228 Value *Idx[2];
11229 Idx[0] = Context->getNullValue(Type::Int32Ty);
11229 Idx[0] = Constant::getNullValue(Type::Int32Ty);
1123011230 Idx[1] = GEP.getOperand(1);
1123111231 GetElementPtrInst *NewGEP =
1123211232 GetElementPtrInst::Create(X, Idx, Idx + 2, GEP.getName());
1129011290
1129111291 // Insert the new GEP instruction.
1129211292 Value *Idx[2];
11293 Idx[0] = Context->getNullValue(Type::Int32Ty);
11293 Idx[0] = Constant::getNullValue(Type::Int32Ty);
1129411294 Idx[1] = NewIdx;
1129511295 Instruction *NewGEP =
1129611296 GetElementPtrInst::Create(X, Idx, Idx + 2, GEP.getName());
1138711387 // Now that I is pointing to the first non-allocation-inst in the block,
1138811388 // insert our getelementptr instruction...
1138911389 //
11390 Value *NullIdx = Context->getNullValue(Type::Int32Ty);
11390 Value *NullIdx = Constant::getNullValue(Type::Int32Ty);
1139111391 Value *Idx[2];
1139211392 Idx[0] = NullIdx;
1139311393 Idx[1] = NullIdx;
1139911399 // allocation.
1140011400 return ReplaceInstUsesWith(AI, V);
1140111401 } else if (isa(AI.getArraySize())) {
11402 return ReplaceInstUsesWith(AI, Context->getNullValue(AI.getType()));
11402 return ReplaceInstUsesWith(AI, Constant::getNullValue(AI.getType()));
1140311403 }
1140411404 }
1140511405
1140811408 // Note that we only do this for alloca's, because malloc should allocate
1140911409 // and return a unique pointer, even for a zero byte allocation.
1141011410 if (TD->getTypeAllocSize(AI.getAllocatedType()) == 0)
11411 return ReplaceInstUsesWith(AI, Context->getNullValue(AI.getType()));
11411 return ReplaceInstUsesWith(AI, Constant::getNullValue(AI.getType()));
1141211412
1141311413 // If the alignment is 0 (unspecified), assign it the preferred alignment.
1141411414 if (AI.getAlignment() == 0)
1152011520 if (Constant *CSrc = dyn_cast(CastOp))
1152111521 if (ASrcTy->getNumElements() != 0) {
1152211522 Value *Idxs[2];
11523 Idxs[0] = Idxs[1] = Context->getNullValue(Type::Int32Ty);
11523 Idxs[0] = Idxs[1] = Constant::getNullValue(Type::Int32Ty);
1152411524 CastOp = ConstantExpr::getGetElementPtr(CSrc, Idxs, 2);
1152511525 SrcTy = cast(CastOp->getType());
1152611526 SrcPTy = SrcTy->getElementType();
1158711587 // an unreachable instruction directly because we cannot modify the
1158811588 // CFG.
1158911589 new StoreInst(UndefValue::get(LI.getType()),
11590 Context->getNullValue(Op->getType()), &LI);
11590 Constant::getNullValue(Op->getType()), &LI);
1159111591 return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType()));
1159211592 }
1159311593 }
1160111601 // this code is not reachable. We do this instead of inserting an
1160211602 // unreachable instruction directly because we cannot modify the CFG.
1160311603 new StoreInst(UndefValue::get(LI.getType()),
11604 Context->getNullValue(Op->getType()), &LI);
11604 Constant::getNullValue(Op->getType()), &LI);
1160511605 return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType()));
1160611606 }
1160711607
1162511625 // an unreachable instruction directly because we cannot modify the
1162611626 // CFG.
1162711627 new StoreInst(UndefValue::get(LI.getType()),
11628 Context->getNullValue(Op->getType()), &LI);
11628 Constant::getNullValue(Op->getType()), &LI);
1162911629 return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType()));
1163011630 }
1163111631
1164111641 if (GlobalVariable *GV = dyn_cast(Op->getUnderlyingObject())){
1164211642 if (GV->isConstant() && GV->hasDefinitiveInitializer()) {
1164311643 if (GV->getInitializer()->isNullValue())
11644 return ReplaceInstUsesWith(LI, Context->getNullValue(LI.getType()));
11644 return ReplaceInstUsesWith(LI, Constant::getNullValue(LI.getType()));
1164511645 else if (isa(GV->getInitializer()))
1164611646 return ReplaceInstUsesWith(LI, UndefValue::get(LI.getType()));
1164711647 }
1169311693 static Instruction *InstCombineStoreToCast(InstCombiner &IC, StoreInst &SI) {
1169411694 User *CI = cast(SI.getOperand(1));
1169511695 Value *CastOp = CI->getOperand(0);
11696 LLVMContext *Context = IC.getContext();
1169711696
1169811697 const Type *DestPTy = cast(CI->getType())->getElementType();
1169911698 const PointerType *SrcTy = dyn_cast(CastOp->getType());
1171511714 // constants.
1171611715 if (isa(SrcPTy) || isa(SrcPTy)) {
1171711716 // Index through pointer.
11718 Constant *Zero = Context->getNullValue(Type::Int32Ty);
11717 Constant *Zero = Constant::getNullValue(Type::Int32Ty);
1171911718 NewGEPIndices.push_back(Zero);
1172011719
1172111720 while (1) {
1218912188 return ReplaceInstUsesWith(EV, UndefValue::get(EV.getType()));
1219012189
1219112190 if (isa(C))
12192 return ReplaceInstUsesWith(EV, Context->getNullValue(EV.getType()));
12191 return ReplaceInstUsesWith(EV, Constant::getNullValue(EV.getType()));
1219312192
1219412193 if (isa(C) || isa(C)) {
1219512194 // Extract the element indexed by the first index out of the constant
1233612335 if (isa(V))
1233712336 return UndefValue::get(PTy->getElementType());
1233812337 else if (isa(V))
12339 return Context->getNullValue(PTy->getElementType());
12338 return Constant::getNullValue(PTy->getElementType());
1234012339 else if (ConstantVector *CP = dyn_cast(V))
1234112340 return CP->getOperand(EltNo);
1234212341 else if (InsertElementInst *III = dyn_cast(V)) {
1237612375
1237712376 // If vector val is constant 0, replace extract with scalar 0.
1237812377 if (isa(EI.getOperand(0)))
12379 return ReplaceInstUsesWith(EI, Context->getNullValue(EI.getType()));
12378 return ReplaceInstUsesWith(EI, Constant::getNullValue(EI.getType()));
1238012379
1238112380 if (ConstantVector *C = dyn_cast(EI.getOperand(0))) {
1238212381 // If vector val is constant with all elements the same, replace EI with
15761576 BasicBlock *LatchBlock = L->getLoopLatch();
15771577 Instruction *IVIncInsertPt = LatchBlock->getTerminator();
15781578
1579 LLVMContext &Context = Preheader->getContext();
1580
1581 Value *CommonBaseV = Context.getNullValue(ReplacedTy);
1579 Value *CommonBaseV = Constant::getNullValue(ReplacedTy);
15821580
15831581 const SCEV *RewriteFactor = SE->getIntegerSCEV(0, ReplacedTy);
15841582 IVExpr ReuseIV(SE->getIntegerSCEV(0, Type::Int32Ty),
17601760 switch (BO->getOpcode()) {
17611761 case Instruction::And: {
17621762 // "and i32 %a, %b" EQ -1 then %a EQ -1 and %b EQ -1
1763 ConstantInt *CI = cast(Context->getAllOnesValue(Ty));
1763 ConstantInt *CI = cast(Constant::getAllOnesValue(Ty));
17641764 if (Canonical == CI) {
17651765 add(CI, Op0, ICmpInst::ICMP_EQ, NewContext);
17661766 add(CI, Op1, ICmpInst::ICMP_EQ, NewContext);
17681768 } break;
17691769 case Instruction::Or: {
17701770 // "or i32 %a, %b" EQ 0 then %a EQ 0 and %b EQ 0
1771 Constant *Zero = Context->getNullValue(Ty);
1771 Constant *Zero = Constant::getNullValue(Ty);
17721772 if (Canonical == Zero) {
17731773 add(Zero, Op0, ICmpInst::ICMP_EQ, NewContext);
17741774 add(Zero, Op1, ICmpInst::ICMP_EQ, NewContext);
17921792 }
17931793 if (Canonical == LHS) {
17941794 if (isa(Canonical))
1795 add(RHS, Context->getNullValue(Ty), ICmpInst::ICMP_EQ,
1795 add(RHS, Constant::getNullValue(Ty), ICmpInst::ICMP_EQ,
17961796 NewContext);
17971797 } else if (isRelatedBy(LHS, Canonical, ICmpInst::ICMP_NE)) {
1798 add(RHS, Context->getNullValue(Ty), ICmpInst::ICMP_NE,
1798 add(RHS, Constant::getNullValue(Ty), ICmpInst::ICMP_NE,
17991799 NewContext);
18001800 }
18011801 } break;
18401840 }
18411841 // TODO: The GEPI indices are all zero. Copy from definition to operand,
18421842 // jumping the type plane as needed.
1843 if (isRelatedBy(GEPI, Context->getNullValue(GEPI->getType()),
1843 if (isRelatedBy(GEPI, Constant::getNullValue(GEPI->getType()),
18441844 ICmpInst::ICMP_NE)) {
18451845 Value *Ptr = GEPI->getPointerOperand();
1846 add(Ptr, Context->getNullValue(Ptr->getType()), ICmpInst::ICMP_NE,
1846 add(Ptr, Constant::getNullValue(Ptr->getType()), ICmpInst::ICMP_NE,
18471847 NewContext);
18481848 }
18491849 } else if (CastInst *CI = dyn_cast(I)) {
18971897 const Type *Ty = BO->getType();
18981898 assert(!Ty->isFPOrFPVector() && "Float in work queue!");
18991899
1900 Constant *Zero = Context->getNullValue(Ty);
1900 Constant *Zero = Constant::getNullValue(Ty);
19011901 Constant *One = ConstantInt::get(Ty, 1);
1902 ConstantInt *AllOnes = cast(Context->getAllOnesValue(Ty));
1902 ConstantInt *AllOnes = cast(Constant::getAllOnesValue(Ty));
19031903
19041904 switch (Opcode) {
19051905 default: break;
21202120 // TODO: The GEPI indices are all zero. Copy from operand to definition,
21212121 // jumping the type plane as needed.
21222122 Value *Ptr = GEPI->getPointerOperand();
2123 if (isRelatedBy(Ptr, Context->getNullValue(Ptr->getType()),
2123 if (isRelatedBy(Ptr, Constant::getNullValue(Ptr->getType()),
21242124 ICmpInst::ICMP_NE)) {
2125 add(GEPI, Context->getNullValue(GEPI->getType()), ICmpInst::ICMP_NE,
2125 add(GEPI, Constant::getNullValue(GEPI->getType()), ICmpInst::ICMP_NE,
21262126 NewContext);
21272127 }
21282128 }
25142514
25152515 void PredicateSimplifier::Forwards::visitAllocaInst(AllocaInst &AI) {
25162516 VRPSolver VRP(VN, IG, UB, VR, PS->DTDFS, PS->modified, &AI);
2517 VRP.add(AI.getContext().getNullValue(AI.getType()),
2517 VRP.add(Constant::getNullValue(AI.getType()),
25182518 &AI, ICmpInst::ICMP_NE);
25192519 VRP.solve();
25202520 }
25252525 if (isa(Ptr)) return;
25262526
25272527 VRPSolver VRP(VN, IG, UB, VR, PS->DTDFS, PS->modified, &LI);
2528 VRP.add(LI.getContext().getNullValue(Ptr->getType()),
2528 VRP.add(Constant::getNullValue(Ptr->getType()),
25292529 Ptr, ICmpInst::ICMP_NE);
25302530 VRP.solve();
25312531 }
25352535 if (isa(Ptr)) return;
25362536
25372537 VRPSolver VRP(VN, IG, UB, VR, PS->DTDFS, PS->modified, &SI);
2538 VRP.add(SI.getContext().getNullValue(Ptr->getType()),
2538 VRP.add(Constant::getNullValue(Ptr->getType()),
25392539 Ptr, ICmpInst::ICMP_NE);
25402540 VRP.solve();
25412541 }
25732573 case Instruction::SDiv: {
25742574 Value *Divisor = BO.getOperand(1);
25752575 VRPSolver VRP(VN, IG, UB, VR, PS->DTDFS, PS->modified, &BO);
2576 VRP.add(BO.getContext().getNullValue(Divisor->getType()),
2576 VRP.add(Constant::getNullValue(Divisor->getType()),
25772577 Divisor, ICmpInst::ICMP_NE);
25782578 VRP.solve();
25792579 break;
200200 static Instruction *LowerNegateToMultiply(Instruction *Neg,
201201 std::map, unsigned> &ValueRankMap,
202202 LLVMContext &Context) {
203 Constant *Cst = Neg->getContext().getAllOnesValue(Neg->getType());
203 Constant *Cst = Constant::getAllOnesValue(Neg->getType());
204204
205205 Instruction *Res = BinaryOperator::CreateMul(Neg->getOperand(1), Cst, "",Neg);
206206 ValueRankMap.erase(Neg);
561561 // Start by folding any constants that we found.
562562 bool IterateOptimization = false;
563563 if (Ops.size() == 1) return Ops[0].Op;
564
565 LLVMContext &Context = I->getContext();
566564
567565 unsigned Opcode = I->getOpcode();
568566
625623 if (FoundX != i) {
626624 if (Opcode == Instruction::And) { // ...&X&~X = 0
627625 ++NumAnnihil;
628 return Context.getNullValue(X->getType());
626 return Constant::getNullValue(X->getType());
629627 } else if (Opcode == Instruction::Or) { // ...|X|~X = -1
630628 ++NumAnnihil;
631 return Context.getAllOnesValue(X->getType());
629 return Constant::getAllOnesValue(X->getType());
632630 }
633631 }
634632 }
647645 assert(Opcode == Instruction::Xor);
648646 if (e == 2) {
649647 ++NumAnnihil;
650 return Context.getNullValue(Ops[0].Op->getType());
648 return Constant::getNullValue(Ops[0].Op->getType());
651649 }
652650 // ... X^X -> ...
653651 Ops.erase(Ops.begin()+i, Ops.begin()+i+2);
672670 // Remove X and -X from the operand list.
673671 if (Ops.size() == 2) {
674672 ++NumAnnihil;
675 return Context.getNullValue(X->getType());
673 return Constant::getNullValue(X->getType());
676674 } else {
677675 Ops.erase(Ops.begin()+i);
678676 if (i < FoundX)
6868
6969 CastInst *AllocaInsertionPoint =
7070 CastInst::Create(Instruction::BitCast,
71 F.getContext().getNullValue(Type::Int32Ty), Type::Int32Ty,
71 Constant::getNullValue(Type::Int32Ty), Type::Int32Ty,
7272 "reg2mem alloca point", I);
7373
7474 // Find the escaped instructions. But don't create stack slots for
812812 if (NonOverdefVal->isUndefined()) {
813813 // Could annihilate value.
814814 if (I.getOpcode() == Instruction::And)
815 markConstant(IV, &I, Context->getNullValue(I.getType()));
815 markConstant(IV, &I, Constant::getNullValue(I.getType()));
816816 else if (const VectorType *PT = dyn_cast(I.getType()))
817 markConstant(IV, &I, Context->getAllOnesValue(PT));
817 markConstant(IV, &I, Constant::getAllOnesValue(PT));
818818 else
819819 markConstant(IV, &I,
820 Context->getAllOnesValue(I.getType()));
820 Constant::getAllOnesValue(I.getType()));
821821 return;
822822 } else {
823823 if (I.getOpcode() == Instruction::And) {
11331133 if (isa(Ptr) &&
11341134 cast(Ptr->getType())->getAddressSpace() == 0) {
11351135 // load null -> null
1136 markConstant(IV, &I, Context->getNullValue(I.getType()));
1136 markConstant(IV, &I, Constant::getNullValue(I.getType()));
11371137 return;
11381138 }
11391139
13751375 // to be handled here, because we don't know whether the top part is 1's
13761376 // or 0's.
13771377 assert(Op0LV.isUndefined());
1378 markForcedConstant(LV, I, Context->getNullValue(ITy));
1378 markForcedConstant(LV, I, Constant::getNullValue(ITy));
13791379 return true;
13801380 case Instruction::Mul:
13811381 case Instruction::And:
13821382 // undef * X -> 0. X could be zero.
13831383 // undef & X -> 0. X could be zero.
1384 markForcedConstant(LV, I, Context->getNullValue(ITy));
1384 markForcedConstant(LV, I, Constant::getNullValue(ITy));
13851385 return true;
13861386
13871387 case Instruction::Or:
13881388 // undef | X -> -1. X could be -1.
13891389 if (const VectorType *PTy = dyn_cast(ITy))
13901390 markForcedConstant(LV, I,
1391 Context->getAllOnesValue(PTy));
1391 Constant::getAllOnesValue(PTy));
13921392 else
1393 markForcedConstant(LV, I, Context->getAllOnesValue(ITy));
1393 markForcedConstant(LV, I, Constant::getAllOnesValue(ITy));
13941394 return true;
13951395
13961396 case Instruction::SDiv:
14031403
14041404 // undef / X -> 0. X could be maxint.
14051405 // undef % X -> 0. X could be 1.
1406 markForcedConstant(LV, I, Context->getNullValue(ITy));
1406 markForcedConstant(LV, I, Constant::getNullValue(ITy));
14071407 return true;
14081408
14091409 case Instruction::AShr:
14241424
14251425 // X >> undef -> 0. X could be 0.
14261426 // X << undef -> 0. X could be 0.
1427 markForcedConstant(LV, I, Context->getNullValue(ITy));
1427 markForcedConstant(LV, I, Constant::getNullValue(ITy));
14281428 return true;
14291429 case Instruction::Select:
14301430 // undef ? X : Y -> X or Y. There could be commonality between X/Y.
328328 std::vector &WorkList) {
329329 DOUT << "Found inst to SROA: " << *AI;
330330 SmallVector ElementAllocas;
331 LLVMContext &Context = AI->getContext();
332331 if (const StructType *ST = dyn_cast(AI->getAllocatedType())) {
333332 ElementAllocas.reserve(ST->getNumContainedTypes());
334333 for (unsigned i = 0, e = ST->getNumContainedTypes(); i != e; ++i) {
417416 // expanded itself once the worklist is rerun.
418417 //
419418 SmallVector NewArgs;
420 NewArgs.push_back(Context.getNullValue(Type::Int32Ty));
419 NewArgs.push_back(Constant::getNullValue(Type::Int32Ty));
421420 NewArgs.append(GEPI->op_begin()+3, GEPI->op_end());
422421 RepValue = GetElementPtrInst::Create(AllocaToUse, NewArgs.begin(),
423422 NewArgs.end(), "", GEPI);
511510 ///
512511 void SROA::isSafeUseOfAllocation(Instruction *User, AllocationInst *AI,
513512 AllocaInfo &Info) {
514 LLVMContext &Context = User->getContext();
515513 if (BitCastInst *C = dyn_cast(User))
516514 return isSafeUseOfBitCastedAllocation(C, AI, Info);
517515
531529
532530 // The GEP is not safe to transform if not of the form "GEP , 0, ".
533531 if (I == E ||
534 I.getOperand() != Context.getNullValue(I.getOperand()->getType())) {
532 I.getOperand() != Constant::getNullValue(I.getOperand()->getType())) {
535533 return MarkUnsafe(Info);
536534 }
537535
765763 const Type *BytePtrTy = MI->getRawDest()->getType();
766764 bool SROADest = MI->getRawDest() == BCInst;
767765
768 Constant *Zero = Context.getNullValue(Type::Int32Ty);
766 Constant *Zero = Constant::getNullValue(Type::Int32Ty);
769767
770768 for (unsigned i = 0, e = NewElts.size(); i != e; ++i) {
771769 // If this is a memcpy/memmove, emit a GEP of the other element address.
820818 Constant *StoreVal;
821819 if (ConstantInt *CI = dyn_cast(MI->getOperand(2))) {
822820 if (CI->isZero()) {
823 StoreVal = Context.getNullValue(EltTy); // 0.0, null, 0, <0,0>
821 StoreVal = Constant::getNullValue(EltTy); // 0.0, null, 0, <0,0>
824822 } else {
825823 // If EltTy is a vector type, get the element type.
826824 const Type *ValTy = EltTy->getScalarType();
10401038 ArrayEltBitOffset = TD->getTypeAllocSizeInBits(ArrayEltTy);
10411039 }
10421040
1043 LLVMContext &Context = LI->getContext();
1044
10451041 Value *ResultVal =
1046 Context.getNullValue(IntegerType::get(AllocaSizeBits));
1042 Constant::getNullValue(IntegerType::get(AllocaSizeBits));
10471043
10481044 for (unsigned i = 0, e = NewElts.size(); i != e; ++i) {
10491045 // Load the value from the alloca. If the NewElt is an aggregate, cast
11881184 if (isa(I.getOperand()))
11891185 return;
11901186
1191 LLVMContext &Context = GEPI->getContext();
1192
11931187 if (NumElements == 1) {
1194 GEPI->setOperand(2, Context.getNullValue(Type::Int32Ty));
1188 GEPI->setOperand(2, Constant::getNullValue(Type::Int32Ty));
11951189 return;
11961190 }
11971191
11991193 // All users of the GEP must be loads. At each use of the GEP, insert
12001194 // two loads of the appropriate indexed GEP and select between them.
12011195 Value *IsOne = new ICmpInst(GEPI, ICmpInst::ICMP_NE, I.getOperand(),
1202 Context.getNullValue(I.getOperand()->getType()),
1196 Constant::getNullValue(I.getOperand()->getType()),
12031197 "isone");
12041198 // Insert the new GEP instructions, which are properly indexed.
12051199 SmallVector Indices(GEPI->op_begin()+1, GEPI->op_end());
1206 Indices[1] = Context.getNullValue(Type::Int32Ty);
1200 Indices[1] = Constant::getNullValue(Type::Int32Ty);
12071201 Value *ZeroIdx = GetElementPtrInst::Create(GEPI->getOperand(0),
12081202 Indices.begin(),
12091203 Indices.end(),
678678 uint64_t i = 0;
679679 while (1) {
680680 if (i == Str.size()) // Didn't find the char. strchr returns null.
681 return Context->getNullValue(CI->getType());
681 return Constant::getNullValue(CI->getType());
682682 // Did we find our match?
683683 if (Str[i] == CharValue)
684684 break;
917917 Value *LHS = CI->getOperand(1), *RHS = CI->getOperand(2);
918918
919919 if (LHS == RHS) // memcmp(s,s,x) -> 0
920 return Context->getNullValue(CI->getType());
920 return Constant::getNullValue(CI->getType());
921921
922922 // Make sure we have a constant length.
923923 ConstantInt *LenC = dyn_cast(CI->getOperand(3));
925925 uint64_t Len = LenC->getZExtValue();
926926
927927 if (Len == 0) // memcmp(s1,s2,0) -> 0
928 return Context->getNullValue(CI->getType());
928 return Constant::getNullValue(CI->getType());
929929
930930 if (Len == 1) { // memcmp(S1,S2,1) -> *LHS - *RHS
931931 Value *LHSV = B.CreateLoad(CastToCStr(LHS, B), "lhsv");
11621162 // Constant fold.
11631163 if (ConstantInt *CI = dyn_cast(Op)) {
11641164 if (CI->getValue() == 0) // ffs(0) -> 0.
1165 return Context->getNullValue(CI->getType());
1165 return Constant::getNullValue(CI->getType());
11661166 return ConstantInt::get(Type::Int32Ty, // ffs(c) -> cttz(c)+1
11671167 CI->getValue().countTrailingZeros()+1);
11681168 }
11751175 V = B.CreateAdd(V, ConstantInt::get(V->getType(), 1), "tmp");
11761176 V = B.CreateIntCast(V, Type::Int32Ty, false, "tmp");
11771177
1178 Value *Cond = B.CreateICmpNE(Op, Context->getNullValue(ArgType), "tmp");
1178 Value *Cond = B.CreateICmpNE(Op, Constant::getNullValue(ArgType), "tmp");
11791179 return B.CreateSelect(Cond, V, ConstantInt::get(Type::Int32Ty, 0));
11801180 }
11811181 };
12341234 // abs(x) -> x >s -1 ? x : -x
12351235 Value *Op = CI->getOperand(1);
12361236 Value *Pos = B.CreateICmpSGT(Op,
1237 Context->getAllOnesValue(Op->getType()),
1237 Constant::getAllOnesValue(Op->getType()),
12381238 "ispos");
12391239 Value *Neg = B.CreateNeg(Op, "neg");
12401240 return B.CreateSelect(Pos, Op, Neg);
13701370 Value *Ptr = CastToCStr(CI->getOperand(1), B);
13711371 B.CreateStore(V, Ptr);
13721372 Ptr = B.CreateGEP(Ptr, ConstantInt::get(Type::Int32Ty, 1), "nul");
1373 B.CreateStore(Context->getNullValue(Type::Int8Ty), Ptr);
1373 B.CreateStore(Constant::getNullValue(Type::Int8Ty), Ptr);
13741374
13751375 return ConstantInt::get(CI->getType(), 1);
13761376 }
251251
252252 // Create a value to return... if the function doesn't return null...
253253 if (BB->getParent()->getReturnType() != Type::VoidTy)
254 RetVal = TI->getContext().getNullValue(
255 BB->getParent()->getReturnType());
254 RetVal = Constant::getNullValue(BB->getParent()->getReturnType());
256255
257256 // Create the return...
258257 NewTI = ReturnInst::Create(RetVal);
238238 DOUT << "inputs: " << inputs.size() << "\n";
239239 DOUT << "outputs: " << outputs.size() << "\n";
240240
241 LLVMContext &Context = header->getContext();
242
243241 // This function returns unsigned, outputs will go back by reference.
244242 switch (NumExitBlocks) {
245243 case 0:
303301 Value *RewriteVal;
304302 if (AggregateArgs) {
305303 Value *Idx[2];
306 Idx[0] = Context.getNullValue(Type::Int32Ty);
304 Idx[0] = Constant::getNullValue(Type::Int32Ty);
307305 Idx[1] = ConstantInt::get(Type::Int32Ty, i);
308306 TerminatorInst *TI = newFunction->begin()->getTerminator();
309307 GetElementPtrInst *GEP =
351349 void CodeExtractor::
352350 emitCallAndSwitchStatement(Function *newFunction, BasicBlock *codeReplacer,
353351 Values &inputs, Values &outputs) {
354 LLVMContext &Context = codeReplacer->getContext();
355
356352 // Emit a call to the new function, passing in: *pointer to struct (if
357353 // aggregating parameters), or plan inputs and allocated memory for outputs
358354 std::vector params, StructValues, ReloadOutputs;
393389
394390 for (unsigned i = 0, e = inputs.size(); i != e; ++i) {
395391 Value *Idx[2];
396 Idx[0] = Context.getNullValue(Type::Int32Ty);
392 Idx[0] = Constant::getNullValue(Type::Int32Ty);
397393 Idx[1] = ConstantInt::get(Type::Int32Ty, i);
398394 GetElementPtrInst *GEP =
399395 GetElementPtrInst::Create(Struct, Idx, Idx + 2,
419415 Value *Output = 0;
420416 if (AggregateArgs) {
421417 Value *Idx[2];
422 Idx[0] = Context.getNullValue(Type::Int32Ty);
418 Idx[0] = Constant::getNullValue(Type::Int32Ty);
423419 Idx[1] = ConstantInt::get(Type::Int32Ty, FirstOut + i);
424420 GetElementPtrInst *GEP
425421 = GetElementPtrInst::Create(Struct, Idx, Idx + 2,
441437
442438 // Now we can emit a switch statement using the call as a value.
443439 SwitchInst *TheSwitch =
444 SwitchInst::Create(Context.getNullValue(Type::Int16Ty),
440 SwitchInst::Create(Constant::getNullValue(Type::Int16Ty),
445441 codeReplacer, 0, codeReplacer);
446442
447443 // Since there may be multiple exits from the original region, make the new
520516 if (DominatesDef) {
521517 if (AggregateArgs) {
522518 Value *Idx[2];
523 Idx[0] = Context.getNullValue(Type::Int32Ty);
519 Idx[0] = Constant::getNullValue(Type::Int32Ty);
524520 Idx[1] = ConstantInt::get(Type::Int32Ty,FirstOut+out);
525521 GetElementPtrInst *GEP =
526522 GetElementPtrInst::Create(OAI, Idx, Idx + 2,
558554 } else {
559555 // Otherwise we must have code extracted an unwind or something, just
560556 // return whatever we want.
561 ReturnInst::Create(Context.getNullValue(OldFnRetTy), TheSwitch);
557 ReturnInst::Create(Constant::getNullValue(OldFnRetTy), TheSwitch);
562558 }
563559
564560 TheSwitch->eraseFromParent();
102102 bool Changed = false;
103103 assert(MallocFunc && FreeFunc && "Pass not initialized!");
104104
105 LLVMContext &Context = BB.getContext();
106
107105 BasicBlock::InstListType &BBIL = BB.getInstList();
108106
109107 const TargetData &TD = getAnalysis();
155153 if (MCall->getType() != Type::VoidTy)
156154 MCast = new BitCastInst(MCall, MI->getType(), "", I);
157155 else
158 MCast = Context.getNullValue(MI->getType());
156 MCast = Constant::getNullValue(MI->getType());
159157
160158 // Replace all uses of the old malloc inst with the cast inst
161159 MI->replaceAllUsesWith(MCast);
114114 // doInitialization - Make sure that there is a prototype for abort in the
115115 // current module.
116116 bool LowerInvoke::doInitialization(Module &M) {
117 LLVMContext &Context = M.getContext();
118
119117 const Type *VoidPtrTy = PointerType::getUnqual(Type::Int8Ty);
120118 AbortMessage = 0;
121119 if (ExpensiveEHSupport) {
142140 if (!(JBListHead = M.getGlobalVariable("llvm.sjljeh.jblist", PtrJBList))) {
143141 JBListHead = new GlobalVariable(M, PtrJBList, false,
144142 GlobalValue::LinkOnceAnyLinkage,
145 Context.getNullValue(PtrJBList),
143 Constant::getNullValue(PtrJBList),
146144 "llvm.sjljeh.jblist");
147145 }
148146
176174 }
177175
178176 void LowerInvoke::createAbortMessage(Module *M) {
179 LLVMContext &Context = M->getContext();
180
181177 if (ExpensiveEHSupport) {
182178 // The abort message for expensive EH support tells the user that the
183179 // program 'unwound' without an 'invoke' instruction.
188184 GlobalVariable *MsgGV = new GlobalVariable(*M, Msg->getType(), true,
189185 GlobalValue::InternalLinkage,
190186 Msg, "abortmsg");
191 std::vector GEPIdx(2, Context.getNullValue(Type::Int32Ty));
187 std::vector GEPIdx(2, Constant::getNullValue(Type::Int32Ty));
192188 AbortMessage = ConstantExpr::getGetElementPtr(MsgGV, &GEPIdx[0], 2);
193189 } else {
194190 // The abort message for cheap EH support tells the user that EH is not
201197 GlobalVariable *MsgGV = new GlobalVariable(*M, Msg->getType(), true,
202198 GlobalValue::InternalLinkage,
203199 Msg, "abortmsg");
204 std::vector GEPIdx(2, Context.getNullValue(Type::Int32Ty));
200 std::vector GEPIdx(2, Constant::getNullValue(Type::Int32Ty));
205201 AbortMessage = ConstantExpr::getGetElementPtr(MsgGV, &GEPIdx[0], 2);
206202 }
207203 }
222218 }
223219
224220 bool LowerInvoke::insertCheapEHSupport(Function &F) {
225 LLVMContext &Context = F.getContext();
226221 bool Changed = false;
227222 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
228223 if (InvokeInst *II = dyn_cast(BB->getTerminator())) {
255250 // Insert a return instruction. This really should be a "barrier", as it
256251 // is unreachable.
257252 ReturnInst::Create(F.getReturnType() == Type::VoidTy ? 0 :
258 Context.getNullValue(F.getReturnType()), UI);
253 Constant::getNullValue(F.getReturnType()), UI);
259254
260255 // Remove the unwind instruction now.
261256 BB->getInstList().erase(UI);
270265 void LowerInvoke::rewriteExpensiveInvoke(InvokeInst *II, unsigned InvokeNo,
271266 AllocaInst *InvokeNum,
272267 SwitchInst *CatchSwitch) {
273 LLVMContext &Context = II->getContext();
274268 ConstantInt *InvokeNoC = ConstantInt::get(Type::Int32Ty, InvokeNo);
275269
276270 // If the unwind edge has phi nodes, split the edge.
290284
291285 BasicBlock::iterator NI = II->getNormalDest()->getFirstNonPHI();
292286 // nonvolatile.
293 new StoreInst(Context.getNullValue(Type::Int32Ty), InvokeNum, false, NI);
287 new StoreInst(Constant::getNullValue(Type::Int32Ty), InvokeNum, false, NI);
294288
295289 // Add a switch case to our unwind block.
296290 CatchSwitch->addCase(InvokeNoC, II->getUnwindDest());
432426 std::vector Unwinds;
433427 std::vector Invokes;
434428
435 LLVMContext &Context = F.getContext();
436
437429 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
438430 if (ReturnInst *RI = dyn_cast(BB->getTerminator())) {
439431 // Remember all return instructions in case we insert an invoke into this
481473 "jblink", F.begin()->begin());
482474
483475 std::vector Idx;
484 Idx.push_back(Context.getNullValue(Type::Int32Ty));
476 Idx.push_back(Constant::getNullValue(Type::Int32Ty));
485477 Idx.push_back(ConstantInt::get(Type::Int32Ty, 1));
486478 OldJmpBufPtr = GetElementPtrInst::Create(JmpBuf, Idx.begin(), Idx.end(),
487479 "OldBuf",
534526 // Compare the return value to zero.
535527 Value *IsNormal = new ICmpInst(EntryBB->getTerminator(),
536528 ICmpInst::ICMP_EQ, SJRet,
537 Context.getNullValue(SJRet->getType()),
529 Constant::getNullValue(SJRet->getType()),
538530 "notunwind");
539531 // Nuke the uncond branch.
540532 EntryBB->getTerminator()->eraseFromParent();
568560
569561 // Load the JBList, if it's null, then there was no catch!
570562 Value *NotNull = new ICmpInst(*UnwindHandler, ICmpInst::ICMP_NE, BufPtr,
571 Context.getNullValue(BufPtr->getType()),
563 Constant::getNullValue(BufPtr->getType()),
572564 "notnull");
573565 BranchInst::Create(UnwindBlock, TermBlock, NotNull, UnwindHandler);
574566
575567 // Create the block to do the longjmp.
576568 // Get a pointer to the jmpbuf and longjmp.
577569 std::vector Idx;
578 Idx.push_back(Context.getNullValue(Type::Int32Ty));
570 Idx.push_back(Constant::getNullValue(Type::Int32Ty));
579571 Idx.push_back(ConstantInt::get(Type::Int32Ty, 0));
580572 Idx[0] = GetElementPtrInst::Create(BufPtr, Idx.begin(), Idx.end(), "JmpBuf",
581573 UnwindBlock);
102102 if (const PointerType *DPTy = dyn_cast(DestTy))
103103 if (PTy->getAddressSpace() == DPTy->getAddressSpace()) {
104104 SmallVector IdxList;
105 IdxList.push_back(Context.getNullValue(Type::Int32Ty));
105 IdxList.push_back(Constant::getNullValue(Type::Int32Ty));
106106 const Type *ElTy = PTy->getElementType();
107107 while (ElTy != DPTy->getElementType()) {
108108 if (const StructType *STy = dyn_cast(ElTy)) {
109109 if (STy->getNumElements() == 0) break;
110110 ElTy = STy->getElementType(0);
111 IdxList.push_back(Context.getNullValue(Type::Int32Ty));
111 IdxList.push_back(Constant::getNullValue(Type::Int32Ty));
112112 } else if (const SequentialType *STy =
113113 dyn_cast(ElTy)) {
114114 if (isa(ElTy)) break; // Can't index into pointers!
133133 SrcTy = NULL;
134134 // First, check for null. Undef is already handled.
135135 if (isa(V))
136 return Context.getNullValue(DestTy);
136 return Constant::getNullValue(DestTy);
137137
138138 if (ConstantVector *CV = dyn_cast(V))
139139 return BitCastConstantVector(Context, CV, DestPTy);
185185 // [us]itofp(undef) = 0, because the result value is bounded.
186186 if (opc == Instruction::ZExt || opc == Instruction::SExt ||
187187 opc == Instruction::UIToFP || opc == Instruction::SIToFP)
188 return Context.getNullValue(DestTy);
188 return Constant::getNullValue(DestTy);
189189 return UndefValue::get(DestTy);
190190 }
191191 // No compile-time operations on this type yet.
336336 if (isa(Val)) // ee(undef, x) -> undef
337337 return UndefValue::get(cast(Val->getType())->getElementType());
338338 if (Val->isNullValue()) // ee(zero, x) -> zero
339 return Context.getNullValue(
339 return Constant::getNullValue(
340340 cast(Val->getType())->getElementType());
341341
342342 if (const ConstantVector *CVal = dyn_cast(Val)) {
388388 Ops.reserve(numOps);
389389 for (unsigned i = 0; i < numOps; ++i) {
390390 const Constant *Op =
391 (idxVal == i) ? Elt : Context.getNullValue(Elt->getType());
391 (idxVal == i) ? Elt : Constant::getNullValue(Elt->getType());
392392 Ops.push_back(const_cast(Op));
393393 }
394394 return ConstantVector::get(Ops);
417417
418418 const Type *EltTy = cast(C->getType())->getElementType();
419419 if (isa(C))
420 return Context.getNullValue(EltTy);
420 return Constant::getNullValue(EltTy);
421421 if (isa(C))
422422 return UndefValue::get(EltTy);
423423 return 0;
476476
477477 if (isa(Agg)) // ev(0, x) -> 0
478478 return
479 Context.getNullValue(ExtractValueInst::getIndexedType(Agg->getType(),
479 Constant::getNullValue(ExtractValueInst::getIndexedType(Agg->getType(),
480480 Idxs,
481481 Idxs + NumIdx));
482482
541541 const Constant *Op =
542542 (*Idxs == i) ?
543543 ConstantFoldInsertValueInstruction(Context,
544 Context.getNullValue(MemberTy),
544 Constant::getNullValue(MemberTy),
545545 Val, Idxs+1, NumIdx-1) :
546 Context.getNullValue(MemberTy);
546 Constant::getNullValue(MemberTy);
547547 Ops[i] = const_cast(Op);
548548 }
549549 if (isa(AggTy))
589589 if (isa(C1) && isa(C2))
590590 // Handle undef ^ undef -> 0 special case. This is a common
591591 // idiom (misuse).
592 return Context.getNullValue(C1->getType());
592 return Constant::getNullValue(C1->getType());
593593 // Fallthrough
594594 case Instruction::Add:
595595 case Instruction::Sub:
596596 return UndefValue::get(C1->getType());
597597 case Instruction::Mul:
598598 case Instruction::And:
599 return Context.getNullValue(C1->getType());
599 return Constant::getNullValue(C1->getType());
600600 case Instruction::UDiv:
601601 case Instruction::SDiv:
602602 case Instruction::URem:
603603 case Instruction::SRem:
604604 if (!isa(C2)) // undef / X -> 0
605 return Context.getNullValue(C1->getType());
605 return Constant::getNullValue(C1->getType());
606606 return const_cast(C2); // X / undef -> undef
607607 case Instruction::Or: // X | undef -> -1
608608 if (const VectorType *PTy = dyn_cast(C1->getType()))
609 return Context.getAllOnesValue(PTy);
610 return Context.getAllOnesValue(C1->getType());
609 return Constant::getAllOnesValue(PTy);
610 return Constant::getAllOnesValue(C1->getType());
611611 case Instruction::LShr:
612612 if (isa(C2) && isa(C1))
613613 return const_cast(C1); // undef lshr undef -> undef
614 return Context.getNullValue(C1->getType()); // X lshr undef -> 0
614 return Constant::getNullValue(C1->getType()); // X lshr undef -> 0
615615 // undef lshr X -> 0
616616 case Instruction::AShr:
617617 if (!isa(C2))
622622 return const_cast(C1); // X ashr undef --> X
623623 case Instruction::Shl:
624624 // undef << X -> 0 or X << undef -> 0
625 return Context.getNullValue(C1->getType());
625 return Constant::getNullValue(C1->getType());
626626 }
627627 }
628628
650650 case Instruction::URem:
651651 case Instruction::SRem:
652652 if (CI2->equalsInt(1))
653 return Context.getNullValue(CI2->getType()); // X % 1 == 0
653 return Constant::getNullValue(CI2->getType()); // X % 1 == 0
654654 if (CI2->equalsInt(0))
655655 return UndefValue::get(CI2->getType()); // X % 0 == undef
656656 break;
687687
688688 // If checking bits we know are clear, return zero.
689689 if ((CI2->getValue() & BitsNotSet) == CI2->getValue())
690 return Context.getNullValue(CI2->getType());
690 return Constant::getNullValue(CI2->getType());
691691 }
692692 }
693693 }
823823 break;
824824 case Instruction::Add:
825825 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
826 C1 = CP1 ? CP1->getOperand(i) : Context.getNullValue(EltTy);
827 C2 = CP2 ? CP2->getOperand(i) : Context.getNullValue(EltTy);
826 C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
827 C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
828828 Res.push_back(ConstantExpr::getAdd(const_cast(C1),
829829 const_cast(C2)));
830830 }
831831 return ConstantVector::get(Res);
832832 case Instruction::FAdd:
833833 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
834 C1 = CP1 ? CP1->getOperand(i) : Context.getNullValue(EltTy);
835 C2 = CP2 ? CP2->getOperand(i) : Context.getNullValue(EltTy);
834 C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
835 C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
836836 Res.push_back(ConstantExpr::getFAdd(const_cast(C1),
837837 const_cast(C2)));
838838 }
839839 return ConstantVector::get(Res);
840840 case Instruction::Sub:
841841 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
842 C1 = CP1 ? CP1->getOperand(i) : Context.getNullValue(EltTy);
843 C2 = CP2 ? CP2->getOperand(i) : Context.getNullValue(EltTy);
842 C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
843 C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
844844 Res.push_back(ConstantExpr::getSub(const_cast(C1),
845845 const_cast(C2)));
846846 }
847847 return ConstantVector::get(Res);
848848 case Instruction::FSub:
849849 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
850 C1 = CP1 ? CP1->getOperand(i) : Context.getNullValue(EltTy);
851 C2 = CP2 ? CP2->getOperand(i) : Context.getNullValue(EltTy);
850 C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
851 C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
852852 Res.push_back(ConstantExpr::getFSub(const_cast(C1),
853853 const_cast(C2)));
854854 }
855855 return ConstantVector::get(Res);
856856 case Instruction::Mul:
857857 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
858 C1 = CP1 ? CP1->getOperand(i) : Context.getNullValue(EltTy);
859 C2 = CP2 ? CP2->getOperand(i) : Context.getNullValue(EltTy);
858 C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
859 C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
860860 Res.push_back(ConstantExpr::getMul(const_cast(C1),
861861 const_cast(C2)));
862862 }
863863 return ConstantVector::get(Res);
864864 case Instruction::FMul:
865865 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
866 C1 = CP1 ? CP1->getOperand(i) : Context.getNullValue(EltTy);
867 C2 = CP2 ? CP2->getOperand(i) : Context.getNullValue(EltTy);
866 C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
867 C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
868868 Res.push_back(ConstantExpr::getFMul(const_cast(C1),
869869 const_cast(C2)));
870870 }
871871 return ConstantVector::get(Res);
872872 case Instruction::UDiv:
873873 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
874 C1 = CP1 ? CP1->getOperand(i) : Context.getNullValue(EltTy);
875 C2 = CP2 ? CP2->getOperand(i) : Context.getNullValue(EltTy);
874 C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
875 C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
876876 Res.push_back(ConstantExpr::getUDiv(const_cast(C1),
877877 const_cast(C2)));
878878 }
879879 return ConstantVector::get(Res);
880880 case Instruction::SDiv:
881881 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
882 C1 = CP1 ? CP1->getOperand(i) : Context.getNullValue(EltTy);
883 C2 = CP2 ? CP2->getOperand(i) : Context.getNullValue(EltTy);
882 C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
883 C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
884884 Res.push_back(ConstantExpr::getSDiv(const_cast(C1),
885885 const_cast(C2)));
886886 }
887887 return ConstantVector::get(Res);
888888 case Instruction::FDiv:
889889 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
890 C1 = CP1 ? CP1->getOperand(i) : Context.getNullValue(EltTy);
891 C2 = CP2 ? CP2->getOperand(i) : Context.getNullValue(EltTy);
890 C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
891 C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
892892 Res.push_back(ConstantExpr::getFDiv(const_cast(C1),
893893 const_cast(C2)));
894894 }
895895 return ConstantVector::get(Res);
896896 case Instruction::URem:
897897 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
898 C1 = CP1 ? CP1->getOperand(i) : Context.getNullValue(EltTy);
899 C2 = CP2 ? CP2->getOperand(i) : Context.getNullValue(EltTy);
898 C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
899 C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
900900 Res.push_back(ConstantExpr::getURem(const_cast(C1),
901901 const_cast(C2)));
902902 }
903903 return ConstantVector::get(Res);
904904 case Instruction::SRem:
905905 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
906 C1 = CP1 ? CP1->getOperand(i) : Context.getNullValue(EltTy);
907 C2 = CP2 ? CP2->getOperand(i) : Context.getNullValue(EltTy);
906 C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
907 C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
908908 Res.push_back(ConstantExpr::getSRem(const_cast(C1),
909909 const_cast(C2)));
910910 }
911911 return ConstantVector::get(Res);
912912 case Instruction::FRem:
913913 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
914 C1 = CP1 ? CP1->getOperand(i) : Context.getNullValue(EltTy);
915 C2 = CP2 ? CP2->getOperand(i) : Context.getNullValue(EltTy);
914 C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
915 C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
916916 Res.push_back(ConstantExpr::getFRem(const_cast(C1),
917917 const_cast(C2)));
918918 }
919919 return ConstantVector::get(Res);
920920 case Instruction::And:
921921 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
922 C1 = CP1 ? CP1->getOperand(i) : Context.getNullValue(EltTy);
923 C2 = CP2 ? CP2->getOperand(i) : Context.getNullValue(EltTy);
922 C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
923 C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
924924 Res.push_back(ConstantExpr::getAnd(const_cast(C1),
925925 const_cast(C2)));
926926 }
927927 return ConstantVector::get(Res);
928928 case Instruction::Or:
929929 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
930 C1 = CP1 ? CP1->getOperand(i) : Context.getNullValue(EltTy);
931 C2 = CP2 ? CP2->getOperand(i) : Context.getNullValue(EltTy);
930 C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
931 C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
932932 Res.push_back(ConstantExpr::getOr(const_cast(C1),
933933 const_cast(C2)));
934934 }
935935 return ConstantVector::get(Res);
936936 case Instruction::Xor:
937937 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
938 C1 = CP1 ? CP1->getOperand(i) : Context.getNullValue(EltTy);
939 C2 = CP2 ? CP2->getOperand(i) : Context.getNullValue(EltTy);
938 C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
939 C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
940940 Res.push_back(ConstantExpr::getXor(const_cast(C1),
941941 const_cast(C2)));
942942 }
943943 return ConstantVector::get(Res);
944944 case Instruction::LShr:
945945 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
946 C1 = CP1 ? CP1->getOperand(i) : Context.getNullValue(EltTy);
947 C2 = CP2 ? CP2->getOperand(i) : Context.getNullValue(EltTy);
946 C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
947 C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
948948 Res.push_back(ConstantExpr::getLShr(const_cast(C1),
949949 const_cast(C2)));
950950 }
951951 return ConstantVector::get(Res);
952952 case Instruction::AShr:
953953 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
954 C1 = CP1 ? CP1->getOperand(i) : Context.getNullValue(EltTy);
955 C2 = CP2 ? CP2->getOperand(i) : Context.getNullValue(EltTy);
954 C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
955 C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
956956 Res.push_back(ConstantExpr::getAShr(const_cast(C1),
957957 const_cast(C2)));
958958 }
959959 return ConstantVector::get(Res);
960960 case Instruction::Shl:
961961 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
962 C1 = CP1 ? CP1->getOperand(i) : Context.getNullValue(EltTy);
963 C2 = CP2 ? CP2->getOperand(i) : Context.getNullValue(EltTy);
962 C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
963 C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
964964 Res.push_back(ConstantExpr::getShl(const_cast(C1),
965965 const_cast(C2)));
966966 }
12351235 if (CE1->getOpcode() == Instruction::ZExt) isSigned = false;
12361236 if (CE1->getOpcode() == Instruction::SExt) isSigned = true;
12371237 return evaluateICmpRelation(Context, CE1Op0,
1238 Context.getNullValue(CE1Op0->getType()),
1238 Constant::getNullValue(CE1Op0->getType()),
12391239 sgnd);
12401240 }
12411241
13781378
13791379 // Fold FCMP_FALSE/FCMP_TRUE unconditionally.
13801380 if (pred == FCmpInst::FCMP_FALSE)
1381 return Context.getNullValue(ResultTy);
1381 return Constant::getNullValue(ResultTy);
13821382
13831383 if (pred == FCmpInst::FCMP_TRUE)
1384 return Context.getAllOnesValue(ResultTy);
1384 return Constant::getAllOnesValue(ResultTy);
13851385
13861386 // Handle some degenerate cases first
13871387 if (isa(C1) || isa(C2))
4242 // Becomes a no-op when multithreading is disabled.
4343 ManagedStatic > ConstantsLock;
4444
45 // Constructor to create a '0' constant of arbitrary type...
46 static const uint64_t zero[2] = {0, 0};
47 Constant* Constant::getNullValue(const Type* Ty) {
48 switch (Ty->getTypeID()) {
49 case Type::IntegerTyID:
50 return ConstantInt::get(Ty, 0);
51 case Type::FloatTyID:
52 return ConstantFP::get(Ty->getContext(), APFloat(APInt(32, 0)));
53 case Type::DoubleTyID:
54 return ConstantFP::get(Ty->getContext(), APFloat(APInt(64, 0)));
55 case Type::X86_FP80TyID:
56 return ConstantFP::get(Ty->getContext(), APFloat(APInt(80, 2, zero)));
57 case Type::FP128TyID:
58 return ConstantFP::get(Ty->getContext(),
59 APFloat(APInt(128, 2, zero), true));
60 case Type::PPC_FP128TyID:
61 return ConstantFP::get(Ty->getContext(), APFloat(APInt(128, 2, zero)));
62 case Type::PointerTyID:
63 return ConstantPointerNull::get(cast(Ty));
64 case Type::StructTyID:
65 case Type::ArrayTyID:
66 case Type::VectorTyID:
67 return ConstantAggregateZero::get(Ty);
68 default:
69 // Function, Label, or Opaque type?
70 assert(!"Cannot create a null constant of that type!");
71 return 0;
72 }
73 }
74
75 Constant* Constant::getAllOnesValue(const Type* Ty) {
76 if (const IntegerType* ITy = dyn_cast(Ty))
77 return ConstantInt::get(Ty->getContext(),
78 APInt::getAllOnesValue(ITy->getBitWidth()));
79
80 std::vector Elts;
81 const VectorType* VTy = cast(Ty);
82 Elts.resize(VTy->getNumElements(), getAllOnesValue(VTy->getElementType()));
83 assert(Elts[0] && "Not a vector integer type!");
84 return cast(ConstantVector::get(Elts));
85 }
86
4587 void Constant::destroyConstantImpl() {
4688 // When a Constant is destroyed, there may be lingering
4789 // references to the constant by other constants in the constant pool. These
147189 const VectorType *VT = cast(getType());
148190 if (isa(this)) {
149191 Elts.assign(VT->getNumElements(),
150 Context.getNullValue(VT->getElementType()));
192 Constant::getNullValue(VT->getElementType()));
151193 return;
152194 }
153195
295337
296338 ConstantFP* ConstantFP::getNegativeZero(const Type* Ty) {
297339 LLVMContext &Context = Ty->getContext();
298 APFloat apf = cast (Context.getNullValue(Ty))->getValueAPF();
340 APFloat apf = cast (Constant::getNullValue(Ty))->getValueAPF();
299341 apf.changeSign();
300342 return get(Context, apf);
301343 }
302344
303345
304346 Constant* ConstantFP::getZeroValueForNegation(const Type* Ty) {
305 LLVMContext &Context = Ty->getContext();
306347 if (const VectorType *PTy = dyn_cast(Ty))
307348 if (PTy->getElementType()->isFloatingPoint()) {
308349 std::vector zeros(PTy->getNumElements(),
313354 if (Ty->isFloatingPoint())
314355 return getNegativeZero(Ty);
315356
316 return Context.getNullValue(Ty);
357 return Constant::getNullValue(Ty);
317358 }
318359
319360
17501791 Constant* ConstantExpr::getSizeOf(const Type* Ty) {
17511792 // sizeof is implemented as: (i64) gep (Ty*)null, 1
17521793 // Note that a non-inbounds gep is used, as null isn't within any object.
1753 LLVMContext &Context = Ty->getContext();
17541794 Constant *GEPIdx = ConstantInt::get(Type::Int32Ty, 1);
17551795 Constant *GEP = getGetElementPtr(
1756 Context.getNullValue(PointerType::getUnqual(Ty)), &GEPIdx, 1);
1796 Constant::getNullValue(PointerType::getUnqual(Ty)), &GEPIdx, 1);
17571797 return getCast(Instruction::PtrToInt, GEP, Type::Int64Ty);
17581798 }
17591799
17601800 Constant* ConstantExpr::getAlignOf(const Type* Ty) {
1761 LLVMContext &Context = Ty->getContext();
17621801 // alignof is implemented as: (i64) gep ({i8,Ty}*)null, 0, 1
17631802 const Type *AligningTy = StructType::get(Type::Int8Ty, Ty, NULL);
1764 Constant *NullPtr = Context.getNullValue(AligningTy->getPointerTo());
1803 Constant *NullPtr = Constant::getNullValue(AligningTy->getPointerTo());
17651804 Constant *Zero = ConstantInt::get(Type::Int32Ty, 0);
17661805 Constant *One = ConstantInt::get(Type::Int32Ty, 1);
17671806 Constant *Indices[2] = { Zero, One };
20282067 Constant* ConstantExpr::getNot(Constant* C) {
20292068 assert(C->getType()->isIntOrIntVector() &&
20302069 "Cannot NOT a nonintegral value!");
2031 LLVMContext &Context = C->getType()->getContext();
2032 return get(Instruction::Xor, C, Context.getAllOnesValue(C->getType()));
2070 return get(Instruction::Xor, C, Constant::getAllOnesValue(C->getType()));
20332071 }
20342072
20352073 Constant* ConstantExpr::getAdd(Constant* C1, Constant* C2) {
326326 /*--.. Operations on constants of any type .................................--*/
327327
328328 LLVMValueRef LLVMConstNull(LLVMTypeRef Ty) {
329 return wrap(getGlobalContext().getNullValue(unwrap(Ty)));
329 return wrap(Constant::getNullValue(unwrap(Ty)));
330330 }
331331
332332 LLVMValueRef LLVMConstAllOnes(LLVMTypeRef Ty) {
333 return wrap(getGlobalContext().getAllOnesValue(unwrap(Ty)));
333 return wrap(Constant::getAllOnesValue(unwrap(Ty)));
334334 }
335335
336336 LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) {
16121612 Instruction *InsertBefore) {
16131613 Constant *C;
16141614 if (const VectorType *PTy = dyn_cast(Op->getType())) {
1615 C = Context.getAllOnesValue(PTy->getElementType());
1615 C = Constant::getAllOnesValue(PTy->getElementType());
16161616 C = ConstantVector::get(
16171617 std::vector(PTy->getNumElements(), C));
16181618 } else {
1619 C = Context.getAllOnesValue(Op->getType());
1619 C = Constant::getAllOnesValue(Op->getType());
16201620 }
16211621
16221622 return new BinaryOperator(Instruction::Xor, Op, C,
16291629 Constant *AllOnes;
16301630 if (const VectorType *PTy = dyn_cast(Op->getType())) {
16311631 // Create a vector of all ones values.
1632 Constant *Elt = Context.getAllOnesValue(PTy->getElementType());
1632 Constant *Elt = Constant::getAllOnesValue(PTy->getElementType());
16331633 AllOnes = ConstantVector::get(
16341634 std::vector(PTy->getNumElements(), Elt));
16351635 } else {
1636 AllOnes = Context.getAllOnesValue(Op->getType());
1636 AllOnes = Constant::getAllOnesValue(Op->getType());
16371637 }
16381638
16391639 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
3131 LLVMContext::LLVMContext() : pImpl(new LLVMContextImpl(*this)) { }
3232 LLVMContext::~LLVMContext() { delete pImpl; }
3333
34 // Constant accessors
35
36 // Constructor to create a '0' constant of arbitrary type...
37 static const uint64_t zero[2] = {0, 0};
38 Constant* LLVMContext::getNullValue(const Type* Ty) {
39 switch (Ty->getTypeID()) {
40 case Type::IntegerTyID:
41 return ConstantInt::get(Ty, 0);
42 case Type::FloatTyID:
43 return ConstantFP::get(Ty->getContext(), APFloat(APInt(32, 0)));
44 case Type::DoubleTyID:
45 return ConstantFP::get(Ty->getContext(), APFloat(APInt(64, 0)));
46 case Type::X86_FP80TyID:
47 return ConstantFP::get(Ty->getContext(), APFloat(APInt(80, 2, zero)));
48 case Type::FP128TyID:
49 return ConstantFP::get(Ty->getContext(),
50 APFloat(APInt(128, 2, zero), true));
51 case Type::PPC_FP128TyID:
52 return ConstantFP::get(Ty->getContext(), APFloat(APInt(128, 2, zero)));
53 case Type::PointerTyID:
54 return ConstantPointerNull::get(cast(Ty));
55 case Type::StructTyID:
56 case Type::ArrayTyID:
57 case Type::VectorTyID:
58 return ConstantAggregateZero::get(Ty);
59 default:
60 // Function, Label, or Opaque type?
61 assert(!"Cannot create a null constant of that type!");
62 return 0;
63 }
64 }
65
66 Constant* LLVMContext::getAllOnesValue(const Type* Ty) {
67 if (const IntegerType* ITy = dyn_cast(Ty))
68 return ConstantInt::get(*this, APInt::getAllOnesValue(ITy->getBitWidth()));
69
70 std::vector Elts;
71 const VectorType* VTy = cast(Ty);
72 Elts.resize(VTy->getNumElements(), getAllOnesValue(VTy->getElementType()));
73 assert(Elts[0] && "Not a vector integer type!");
74 return cast(ConstantVector::get(Elts));
75 }
76
7734 // MDNode accessors
7835 MDNode* LLVMContext::getMDNode(Value* const* Vals, unsigned NumVals) {
7936 return pImpl->getMDNode(Vals, NumVals);
297297 if (isa(BBTerm->getType()))
298298 BBTerm->replaceAllUsesWith(UndefValue::get(BBTerm->getType()));
299299 else if (BB->getTerminator()->getType() != Type::VoidTy)
300 BBTerm->replaceAllUsesWith(
301 BD.getContext().getNullValue(BBTerm->getType()));
300 BBTerm->replaceAllUsesWith(Constant::getNullValue(BBTerm->getType()));
302301
303302 // Replace the old terminator instruction.
304303 BB->getInstList().pop_back();
7373 if (isa(TheInst->getType()))
7474 TheInst->replaceAllUsesWith(UndefValue::get(TheInst->getType()));
7575 else if (TheInst->getType() != Type::VoidTy)
76 TheInst->replaceAllUsesWith(Context.getNullValue(TheInst->getType()));
76 TheInst->replaceAllUsesWith(Constant::getNullValue(TheInst->getType()));
7777
7878 // Remove the instruction from the program.
7979 TheInst->getParent()->getInstList().erase(TheInst);
649649 ///
650650 static void CleanupAndPrepareModules(BugDriver &BD, Module *&Test,
651651 Module *Safe) {
652 LLVMContext &Context = BD.getContext();
653
654652 // Clean up the modules, removing extra cruft that we don't need anymore...
655653 Test = BD.performFinalCleanups(Test);
656654
722720 // sbyte* so it matches the signature of the resolver function.
723721
724722 // GetElementPtr *funcName, ulong 0, ulong 0
725 std::vector GEPargs(2, Context.getNullValue(Type::Int32Ty));
723 std::vector GEPargs(2, Constant::getNullValue(Type::Int32Ty));
726724 Value *GEP =
727725 ConstantExpr::getGetElementPtr(funcName, &GEPargs[0], 2);
728726 std::vector ResolverArgs;
5959 for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I)
6060 if (CallInst *CI = dyn_cast(I)) {
6161 if (!CI->use_empty())
62 CI->replaceAllUsesWith(BB.getContext().getNullValue(CI->getType()));
62 CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
6363 CI->getParent()->getInstList().erase(CI);
6464 break;
6565 }
294294
295295 private:
296296 virtual void deleted() {
297 getValPtr()->replaceAllUsesWith(Context->getNullValue(Type::Int32Ty));
297 getValPtr()->replaceAllUsesWith(Constant::getNullValue(Type::Int32Ty));
298298 setValPtr(NULL);
299299 }
300300 virtual void allUsesReplacedWith(Value *new_value) {