llvm.org GIT mirror llvm / 1fd7096
Change ConstantArray to 2.5 API. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@77347 91177308-0d34-0410-b5e6-96231b3b80d8 Owen Anderson 11 years ago
24 changed file(s) with 177 addition(s) and 221 deletion(s). Raw diff Collapse all Expand all
124124 {
125125 //@aberrormsg = internal constant [%d x i8] c"\00"
126126 Constant *msg_0 =
127 C.getConstantArray("Error: The head has left the tape.", true);
127 ConstantArray::get("Error: The head has left the tape.", true);
128128
129129 GlobalVariable *aberrormsg = new GlobalVariable(
130130 *module,
326326 friend struct ConstantCreator
327327 std::vector >;
328328 ConstantArray(const ConstantArray &); // DO NOT IMPLEMENT
329 friend class LLVMContextImpl;
330329 protected:
331330 ConstantArray(const ArrayType *T, const std::vector &Val);
332331 public:
332 // ConstantArray accessors
333 static Constant* get(const ArrayType* T, const std::vector& V);
334 static Constant* get(const ArrayType* T, Constant* const* Vals,
335 unsigned NumVals);
336
337 /// This method constructs a ConstantArray and initializes it with a text
338 /// string. The default behavior (AddNull==true) causes a null terminator to
339 /// be placed at the end of the array. This effectively increases the length
340 /// of the array by one (you've been warned). However, in some situations
341 /// this is not desired so if AddNull==false then the string is copied without
342 /// null termination.
343 static Constant* get(const StringRef &Initializer, bool AddNull = true);
344
333345 /// Transparently provide more efficient getOperand methods.
334346 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
335347
5757 friend class ConstantInt;
5858 friend class ConstantFP;
5959 friend class ConstantStruct;
60 friend class ConstantArray;
6061 public:
6162 LLVMContext();
6263 ~LLVMContext();
8182
8283 // ConstantAggregateZero accessors
8384 ConstantAggregateZero* getConstantAggregateZero(const Type* Ty);
84
85 // ConstantArray accessors
86 Constant* getConstantArray(const ArrayType* T,
87 const std::vector& V);
88 Constant* getConstantArray(const ArrayType* T, Constant* const* Vals,
89 unsigned NumVals);
90
91 /// This method constructs a ConstantArray and initializes it with a text
92 /// string. The default behavior (AddNull==true) causes a null terminator to
93 /// be placed at the end of the array. This effectively increases the length
94 /// of the array by one (you've been warned). However, in some situations
95 /// this is not desired so if AddNull==false then the string is copied without
96 /// null termination.
97 Constant* getConstantArray(const StringRef &Initializer,
98 bool AddNull = true);
9985
10086 // ConstantExpr accessors
10187 Constant* getConstantExpr(unsigned Opcode, Constant* C1, Constant* C2);
224210 void erase(MDString *M);
225211 void erase(MDNode *M);
226212 void erase(ConstantAggregateZero *Z);
227 void erase(ConstantArray *Z);
228213 void erase(ConstantVector *V);
229
230 // RAUW helpers
231 Constant *replaceUsesOfWithOnConstant(ConstantArray *CA,
232 Value *From, Value *To, Use *U);
233214 };
234215
235216 /// FOR BACKWARDS COMPATIBILITY - Returns a global context.
412412 return CreateConstGEP2_32(Ptr, 0, Idx, Name);
413413 }
414414 Value *CreateGlobalString(const char *Str = "", const char *Name = "") {
415 Constant *StrConstant = Context.getConstantArray(Str, true);
415 Constant *StrConstant = ConstantArray::get(Str, true);
416416 Module &M = *BB->getParent()->getParent();
417417 GlobalVariable *gv = new GlobalVariable(M,
418418 StrConstant->getType(),
498498 return Slot = VMContext.getConstantPointerNull(DestTy);
499499
500500 // Construct string as an llvm constant.
501 Constant *ConstStr = VMContext.getConstantArray(String);
501 Constant *ConstStr = ConstantArray::get(String);
502502
503503 // Otherwise create and return a new string global.
504504 GlobalVariable *StrGV = new GlobalVariable(M, ConstStr->getType(), true,
520520 for (unsigned i = 0; i != NumTys; ++i)
521521 Elts.push_back(getCastToEmpty(Tys[i]));
522522
523 Constant *Init = VMContext.getConstantArray(VMContext.getArrayType(EmptyStructPtr,
523 Constant *Init = ConstantArray::get(VMContext.getArrayType(EmptyStructPtr,
524524 Elts.size()),
525525 Elts.data(), Elts.size());
526526 // If we already have this array, just return the uniqued version.
18101810 " is not of type '" +Elts[0]->getType()->getDescription());
18111811 }
18121812
1813 ID.ConstantVal = Context.getConstantArray(ATy, Elts.data(), Elts.size());
1813 ID.ConstantVal = ConstantArray::get(ATy, Elts.data(), Elts.size());
18141814 ID.Kind = ValID::t_Constant;
18151815 return false;
18161816 }
18171817 case lltok::kw_c: // c "foo"
18181818 Lex.Lex();
1819 ID.ConstantVal = Context.getConstantArray(Lex.getStrVal(), false);
1819 ID.ConstantVal = ConstantArray::get(Lex.getStrVal(), false);
18201820 if (ParseToken(lltok::StringConstant, "expected string")) return true;
18211821 ID.Kind = ValID::t_Constant;
18221822 return false;
287287 // Make the new constant.
288288 Constant *NewC;
289289 if (ConstantArray *UserCA = dyn_cast(UserC)) {
290 NewC = Context.getConstantArray(UserCA->getType(), &NewOps[0],
290 NewC = ConstantArray::get(UserCA->getType(), &NewOps[0],
291291 NewOps.size());
292292 } else if (ConstantStruct *UserCS = dyn_cast(UserC)) {
293293 NewC = ConstantStruct::get(&NewOps[0], NewOps.size(),
929929 const Type *EltTy = ATy->getElementType();
930930 for (unsigned i = 0; i != Size; ++i)
931931 Elts.push_back(ValueList.getConstantFwdRef(Record[i], EltTy));
932 V = Context.getConstantArray(ATy, Elts);
932 V = ConstantArray::get(ATy, Elts);
933933 } else if (const VectorType *VTy = dyn_cast(CurTy)) {
934934 const Type *EltTy = VTy->getElementType();
935935 for (unsigned i = 0; i != Size; ++i)
951951 std::vector Elts;
952952 for (unsigned i = 0; i != Size; ++i)
953953 Elts.push_back(ConstantInt::get(EltTy, Record[i]));
954 V = Context.getConstantArray(ATy, Elts);
954 V = ConstantArray::get(ATy, Elts);
955955 break;
956956 }
957957 case bitc::CST_CODE_CSTRING: { // CSTRING: [values]
966966 for (unsigned i = 0; i != Size; ++i)
967967 Elts.push_back(ConstantInt::get(EltTy, Record[i]));
968968 Elts.push_back(Context.getNullValue(EltTy));
969 V = Context.getConstantArray(ATy, Elts);
969 V = ConstantArray::get(ATy, Elts);
970970 break;
971971 }
972972 case bitc::CST_CODE_CE_BINOP: { // CE_BINOP: [opcode, opval, opval]
58115811 const TargetData &TD = *TLI.getTargetData();
58125812
58135813 // Create a ConstantArray of the two constants.
5814 Constant *CA = DAG.getContext()->getConstantArray(
5814 Constant *CA = ConstantArray::get(
58155815 DAG.getContext()->getArrayType(FPTy, 2), Elts, 2);
58165816 SDValue CPIdx = DAG.getConstantPool(CA, TLI.getPointerTy(),
58175817 TD.getPrefTypeAlignment(FPTy));
208208
209209 Constant *DescriptorElts[] = {
210210 ConstantStruct::get(BaseElts, 2),
211 Context.getConstantArray(Context.getArrayType(VoidPtr, NumMeta),
211 ConstantArray::get(Context.getArrayType(VoidPtr, NumMeta),
212212 Metadata.begin(), NumMeta)
213213 };
214214
368368 Operands[i] =cast(RemapOperand(CPA->getOperand(i), ValueMap,
369369 Context));
370370 Result =
371 Context.getConstantArray(cast(CPA->getType()), Operands);
371 ConstantArray::get(cast(CPA->getType()), Operands);
372372 } else if (const ConstantStruct *CPS = dyn_cast(CPV)) {
373373 std::vector Operands(CPS->getNumOperands());
374374 for (unsigned i = 0, e = CPS->getNumOperands(); i != e; ++i)
11851185 for (unsigned i = 0, e = T2->getNumElements(); i != e; ++i)
11861186 Inits.push_back(CV);
11871187 }
1188 NG->setInitializer(Context.getConstantArray(NewType, Inits));
1188 NG->setInitializer(ConstantArray::get(NewType, Inits));
11891189 Inits.clear();
11901190
11911191 // Replace any uses of the two global variables with uses of the new
2828 else
2929 ReadOnlySection = getOrCreateSection("\t.cp.rodata", false,
3030 SectionKind::ReadOnly);
31 }
31 }
109109 AUGs.push_back(Context.getConstantExprBitCast(*GI, SBP));
110110 }
111111 ArrayType *AT = Context.getArrayType(SBP, AUGs.size());
112 Constant *Init = Context.getConstantArray(AT, AUGs);
112 Constant *Init = ConstantArray::get(AT, AUGs);
113113 GlobalValue *gv = new GlobalVariable(M, AT, false,
114114 GlobalValue::AppendingLinkage,
115115 Init, "llvm.used");
19671967 // Create the array initializer.
19681968 const Type *StructTy =
19691969 cast(GCL->getType()->getElementType())->getElementType();
1970 Constant *CA = Context.getConstantArray(ArrayType::get(StructTy,
1970 Constant *CA = ConstantArray::get(ArrayType::get(StructTy,
19711971 CAList.size()), CAList);
19721972
19731973 // If we didn't change the number of elements, don't create a new GV.
20932093 assert(CI->getZExtValue() < ATy->getNumElements());
20942094 Elts[CI->getZExtValue()] =
20952095 EvaluateStoreInto(Elts[CI->getZExtValue()], Val, Addr, OpNo+1, Context);
2096 return Context.getConstantArray(ATy, Elts);
2096 return ConstantArray::get(ATy, Elts);
20972097 }
20982098 }
20992099
12891289 // Create a string literal with no \n on it. We expect the constant merge
12901290 // pass to be run after this pass, to merge duplicate strings.
12911291 FormatStr.erase(FormatStr.end()-1);
1292 Constant *C = Context->getConstantArray(FormatStr, true);
1292 Constant *C = ConstantArray::get(FormatStr, true);
12931293 C = new GlobalVariable(*Callee->getParent(), C->getType(), true,
12941294 GlobalVariable::InternalLinkage, C, "str");
12951295 EmitPutS(C, B);
182182 // The abort message for expensive EH support tells the user that the
183183 // program 'unwound' without an 'invoke' instruction.
184184 Constant *Msg =
185 Context.getConstantArray("ERROR: Exception thrown, but not caught!\n");
185 ConstantArray::get("ERROR: Exception thrown, but not caught!\n");
186186 AbortMessageLength = Msg->getNumOperands()-1; // don't include \0
187187
188188 GlobalVariable *MsgGV = new GlobalVariable(*M, Msg->getType(), true,
194194 // The abort message for cheap EH support tells the user that EH is not
195195 // enabled.
196196 Constant *Msg =
197 Context.getConstantArray("Exception handler needed, but not enabled."
197 ConstantArray::get("Exception handler needed, but not enabled."
198198 "Recompile program with -enable-correct-eh-support.\n");
199199 AbortMessageLength = Msg->getNumOperands()-1; // don't include \0
200200
5555 Values.push_back(cast(MV));
5656 for (++i; i != e; ++i)
5757 Values.push_back(cast(MapValue(*i, VM, Context)));
58 return VM[V] = Context.getConstantArray(CA->getType(), Values);
58 return VM[V] = ConstantArray::get(CA->getType(), Values);
5959 }
6060 }
6161 return VM[V] = C;
520520 if (isa(AggTy))
521521 return ConstantStruct::get(Ops);
522522 else
523 return Context.getConstantArray(cast(AggTy), Ops);
523 return ConstantArray::get(cast(AggTy), Ops);
524524 }
525525 if (isa(Agg)) {
526526 // Insertion of constant into aggregate zero
549549 if (isa(AggTy))
550550 return ConstantStruct::get(Ops);
551551 else
552 return Context.getConstantArray(cast(AggTy), Ops);
552 return ConstantArray::get(cast(AggTy), Ops);
553553 }
554554 if (isa(Agg) || isa(Agg)) {
555555 // Insertion of constant into aggregate constant
566566 if (isa(Agg->getType()))
567567 C = ConstantStruct::get(Ops);
568568 else
569 C = Context.getConstantArray(cast(Agg->getType()), Ops);
569 C = ConstantArray::get(cast(Agg->getType()), Ops);
570570 return C;
571571 }
572572
373373 *OL = C;
374374 }
375375 }
376
377 Constant *ConstantArray::get(const ArrayType *Ty,
378 const std::vector &V) {
379 LLVMContextImpl *pImpl = Ty->getContext().pImpl;
380 // If this is an all-zero array, return a ConstantAggregateZero object
381 if (!V.empty()) {
382 Constant *C = V[0];
383 if (!C->isNullValue()) {
384 // Implicitly locked.
385 return pImpl->ArrayConstants.getOrCreate(Ty, V);
386 }
387 for (unsigned i = 1, e = V.size(); i != e; ++i)
388 if (V[i] != C) {
389 // Implicitly locked.
390 return pImpl->ArrayConstants.getOrCreate(Ty, V);
391 }
392 }
393
394 return Ty->getContext().getConstantAggregateZero(Ty);
395 }
396
397
398 Constant* ConstantArray::get(const ArrayType* T, Constant* const* Vals,
399 unsigned NumVals) {
400 // FIXME: make this the primary ctor method.
401 return get(T, std::vector(Vals, Vals+NumVals));
402 }
403
404 /// ConstantArray::get(const string&) - Return an array that is initialized to
405 /// contain the specified string. If length is zero then a null terminator is
406 /// added to the specified string so that it may be used in a natural way.
407 /// Otherwise, the length parameter specifies how much of the string to use
408 /// and it won't be null terminated.
409 ///
410 Constant* ConstantArray::get(const StringRef &Str, bool AddNull) {
411 std::vector ElementVals;
412 for (unsigned i = 0; i < Str.size(); ++i)
413 ElementVals.push_back(ConstantInt::get(Type::Int8Ty, Str[i]));
414
415 // Add a null terminator to the string...
416 if (AddNull) {
417 ElementVals.push_back(ConstantInt::get(Type::Int8Ty, 0));
418 }
419
420 ArrayType *ATy = ArrayType::get(Type::Int8Ty, ElementVals.size());
421 return get(ATy, ElementVals);
422 }
423
376424
377425
378426 ConstantStruct::ConstantStruct(const StructType *T,
942990 ///
943991 void ConstantArray::destroyConstant() {
944992 // Implicitly locked.
945 getType()->getContext().erase(this);
993 getType()->getContext().pImpl->ArrayConstants.remove(this);
946994 destroyConstantImpl();
947995 }
948996
19061954 /// single invocation handles all 1000 uses. Handling them one at a time would
19071955 /// work, but would be really slow because it would have to unique each updated
19081956 /// array instance.
1957
1958 static std::vector getValType(ConstantArray *CA) {
1959 std::vector Elements;
1960 Elements.reserve(CA->getNumOperands());
1961 for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i)
1962 Elements.push_back(cast(CA->getOperand(i)));
1963 return Elements;
1964 }
1965
1966
19091967 void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To,
19101968 Use *U) {
1911 Constant *Replacement =
1912 getType()->getContext().replaceUsesOfWithOnConstant(this, From, To, U);
1913
1914 if (!Replacement) return;
1969 assert(isa(To) && "Cannot make Constant refer to non-constant!");
1970 Constant *ToC = cast(To);
1971
1972 LLVMContext &Context = getType()->getContext();
1973 LLVMContextImpl *pImpl = Context.pImpl;
1974
1975 std::pair Lookup;
1976 Lookup.first.first = getType();
1977 Lookup.second = this;
1978
1979 std::vector &Values = Lookup.first.second;
1980 Values.reserve(getNumOperands()); // Build replacement array.
1981
1982 // Fill values with the modified operands of the constant array. Also,
1983 // compute whether this turns into an all-zeros array.
1984 bool isAllZeros = false;
1985 unsigned NumUpdated = 0;
1986 if (!ToC->isNullValue()) {
1987 for (Use *O = OperandList, *E = OperandList+getNumOperands(); O != E; ++O) {
1988 Constant *Val = cast(O->get());
1989 if (Val == From) {
1990 Val = ToC;
1991 ++NumUpdated;
1992 }
1993 Values.push_back(Val);
1994 }
1995 } else {
1996 isAllZeros = true;
1997 for (Use *O = OperandList, *E = OperandList+getNumOperands();O != E; ++O) {
1998 Constant *Val = cast(O->get());
1999 if (Val == From) {
2000 Val = ToC;
2001 ++NumUpdated;
2002 }
2003 Values.push_back(Val);
2004 if (isAllZeros) isAllZeros = Val->isNullValue();
2005 }
2006 }
2007
2008 Constant *Replacement = 0;
2009 if (isAllZeros) {
2010 Replacement = Context.getConstantAggregateZero(getType());
2011 } else {
2012 // Check to see if we have this array type already.
2013 sys::SmartScopedWriter Writer(pImpl->ConstantsLock);
2014 bool Exists;
2015 LLVMContextImpl::ArrayConstantsTy::MapTy::iterator I =
2016 pImpl->ArrayConstants.InsertOrGetItem(Lookup, Exists);
2017
2018 if (Exists) {
2019 Replacement = I->second;
2020 } else {
2021 // Okay, the new shape doesn't exist in the system yet. Instead of
2022 // creating a new constant array, inserting it, replaceallusesof'ing the
2023 // old with the new, then deleting the old... just update the current one
2024 // in place!
2025 pImpl->ArrayConstants.MoveConstantToNewSlot(this, I);
2026
2027 // Update to the new value. Optimize for the case when we have a single
2028 // operand that we're changing, but handle bulk updates efficiently.
2029 if (NumUpdated == 1) {
2030 unsigned OperandToUpdate = U - OperandList;
2031 assert(getOperand(OperandToUpdate) == From &&
2032 "ReplaceAllUsesWith broken!");
2033 setOperand(OperandToUpdate, ToC);
2034 } else {
2035 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
2036 if (getOperand(i) == From)
2037 setOperand(i, ToC);
2038 }
2039 return;
2040 }
2041 }
19152042
19162043 // Otherwise, I do need to replace this with an existing value.
19172044 assert(Replacement != this && "I didn't contain From!");
401401 int DontNullTerminate) {
402402 /* Inverted the sense of AddNull because ', 0)' is a
403403 better mnemonic for null termination than ', 1)'. */
404 return wrap(getGlobalContext().getConstantArray(std::string(Str, Length),
404 return wrap(ConstantArray::get(std::string(Str, Length),
405405 DontNullTerminate == 0));
406406 }
407407
408408 LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy,
409409 LLVMValueRef *ConstantVals, unsigned Length) {
410 return wrap(getGlobalContext().getConstantArray(
410 return wrap(ConstantArray::get(
411411 getGlobalContext().getArrayType(unwrap(ElementTy), Length),
412412 unwrap(ConstantVals, Length),
413413 Length));
102102 return pImpl->getConstantAggregateZero(Ty);
103103 }
104104
105
106 // ConstantArray accessors.
107 Constant* LLVMContext::getConstantArray(const ArrayType* T,
108 const std::vector& V) {
109 return pImpl->getConstantArray(T, V);
110 }
111
112 Constant* LLVMContext::getConstantArray(const ArrayType* T,
113 Constant* const* Vals,
114 unsigned NumVals) {
115 // FIXME: make this the primary ctor method.
116 return getConstantArray(T, std::vector(Vals, Vals+NumVals));
117 }
118
119 /// ConstantArray::get(const string&) - Return an array that is initialized to
120 /// contain the specified string. If length is zero then a null terminator is
121 /// added to the specified string so that it may be used in a natural way.
122 /// Otherwise, the length parameter specifies how much of the string to use
123 /// and it won't be null terminated.
124 ///
125 Constant* LLVMContext::getConstantArray(const StringRef &Str,
126 bool AddNull) {
127 std::vector ElementVals;
128 for (unsigned i = 0; i < Str.size(); ++i)
129 ElementVals.push_back(ConstantInt::get(Type::Int8Ty, Str[i]));
130
131 // Add a null terminator to the string...
132 if (AddNull) {
133 ElementVals.push_back(ConstantInt::get(Type::Int8Ty, 0));
134 }
135
136 ArrayType *ATy = getArrayType(Type::Int8Ty, ElementVals.size());
137 return getConstantArray(ATy, ElementVals);
138 }
139
140
141105 // ConstantExpr accessors.
142106 Constant* LLVMContext::getConstantExpr(unsigned Opcode, Constant* C1,
143107 Constant* C2) {
524488 pImpl->erase(Z);
525489 }
526490
527 void LLVMContext::erase(ConstantArray *C) {
528 pImpl->erase(C);
529 }
530
531491 void LLVMContext::erase(ConstantVector *V) {
532492 pImpl->erase(V);
533493 }
534
535 Constant *LLVMContext::replaceUsesOfWithOnConstant(ConstantArray *CA,
536 Value *From, Value *To, Use *U) {
537 return pImpl->replaceUsesOfWithOnConstant(CA, From, To, U);
538 }
1919 using namespace llvm;
2020
2121 static char getValType(ConstantAggregateZero *CPZ) { return 0; }
22
23 static std::vector getValType(ConstantArray *CA) {
24 std::vector Elements;
25 Elements.reserve(CA->getNumOperands());
26 for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i)
27 Elements.push_back(cast(CA->getOperand(i)));
28 return Elements;
29 }
3022
3123 static std::vector getValType(ConstantVector *CP) {
3224 std::vector Elements;
8476 return AggZeroConstants.getOrCreate(Ty, 0);
8577 }
8678
87 Constant *LLVMContextImpl::getConstantArray(const ArrayType *Ty,
88 const std::vector &V) {
89 // If this is an all-zero array, return a ConstantAggregateZero object
90 if (!V.empty()) {
91 Constant *C = V[0];
92 if (!C->isNullValue()) {
93 // Implicitly locked.
94 return ArrayConstants.getOrCreate(Ty, V);
95 }
96 for (unsigned i = 1, e = V.size(); i != e; ++i)
97 if (V[i] != C) {
98 // Implicitly locked.
99 return ArrayConstants.getOrCreate(Ty, V);
100 }
101 }
102
103 return Context.getConstantAggregateZero(Ty);
104 }
105
10679 Constant *LLVMContextImpl::getConstantVector(const VectorType *Ty,
10780 const std::vector &V) {
10881 assert(!V.empty() && "Vectors can't be empty");
145118 AggZeroConstants.remove(Z);
146119 }
147120
148 void LLVMContextImpl::erase(ConstantArray *C) {
149 ArrayConstants.remove(C);
150 }
151
152121 void LLVMContextImpl::erase(ConstantVector *V) {
153122 VectorConstants.remove(V);
154123 }
155
156 // *** RAUW helpers ***
157
158 Constant *LLVMContextImpl::replaceUsesOfWithOnConstant(ConstantArray *CA,
159 Value *From, Value *To, Use *U) {
160 assert(isa(To) && "Cannot make Constant refer to non-constant!");
161 Constant *ToC = cast(To);
162
163 std::pair Lookup;
164 Lookup.first.first = CA->getType();
165 Lookup.second = CA;
166
167 std::vector &Values = Lookup.first.second;
168 Values.reserve(CA->getNumOperands()); // Build replacement array.
169
170 // Fill values with the modified operands of the constant array. Also,
171 // compute whether this turns into an all-zeros array.
172 bool isAllZeros = false;
173 unsigned NumUpdated = 0;
174 if (!ToC->isNullValue()) {
175 for (Use *O = CA->OperandList, *E = CA->OperandList + CA->getNumOperands();
176 O != E; ++O) {
177 Constant *Val = cast(O->get());
178 if (Val == From) {
179 Val = ToC;
180 ++NumUpdated;
181 }
182 Values.push_back(Val);
183 }
184 } else {
185 isAllZeros = true;
186 for (Use *O = CA->OperandList, *E = CA->OperandList + CA->getNumOperands();
187 O != E; ++O) {
188 Constant *Val = cast(O->get());
189 if (Val == From) {
190 Val = ToC;
191 ++NumUpdated;
192 }
193 Values.push_back(Val);
194 if (isAllZeros) isAllZeros = Val->isNullValue();
195 }
196 }
197
198 Constant *Replacement = 0;
199 if (isAllZeros) {
200 Replacement = Context.getConstantAggregateZero(CA->getType());
201 } else {
202 // Check to see if we have this array type already.
203 sys::SmartScopedWriter Writer(ConstantsLock);
204 bool Exists;
205 ArrayConstantsTy::MapTy::iterator I =
206 ArrayConstants.InsertOrGetItem(Lookup, Exists);
207
208 if (Exists) {
209 Replacement = I->second;
210 } else {
211 // Okay, the new shape doesn't exist in the system yet. Instead of
212 // creating a new constant array, inserting it, replaceallusesof'ing the
213 // old with the new, then deleting the old... just update the current one
214 // in place!
215 ArrayConstants.MoveConstantToNewSlot(CA, I);
216
217 // Update to the new value. Optimize for the case when we have a single
218 // operand that we're changing, but handle bulk updates efficiently.
219 if (NumUpdated == 1) {
220 unsigned OperandToUpdate = U - CA->OperandList;
221 assert(CA->getOperand(OperandToUpdate) == From &&
222 "ReplaceAllUsesWith broken!");
223 CA->setOperand(OperandToUpdate, ToC);
224 } else {
225 for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i)
226 if (CA->getOperand(i) == From)
227 CA->setOperand(i, ToC);
228 }
229 return 0;
230 }
231 }
232
233 return Replacement;
234 }
8787 std::vector C;
8888 for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
8989 C.push_back(cast(OldC->getOperand(i)));
90 Constant *New = NewTy->getContext().getConstantArray(NewTy, C);
90 Constant *New = ConstantArray::get(NewTy, C);
9191 assert(New != OldC && "Didn't replace constant??");
9292 OldC->uncheckedReplaceAllUsesWith(New);
9393 OldC->destroyConstant(); // This constant is now dead, destroy it.
458458 friend class ConstantInt;
459459 friend class ConstantFP;
460460 friend class ConstantStruct;
461 friend class ConstantArray;
461462 public:
462463 LLVMContextImpl(LLVMContext &C);
463464
466467 MDNode *getMDNode(Value*const* Vals, unsigned NumVals);
467468
468469 ConstantAggregateZero *getConstantAggregateZero(const Type *Ty);
469
470 Constant *getConstantArray(const ArrayType *Ty,
471 const std::vector &V);
472470
473471 Constant *getConstantVector(const VectorType *Ty,
474472 const std::vector &V);
490488 void erase(MDString *M);
491489 void erase(MDNode *M);
492490 void erase(ConstantAggregateZero *Z);
493 void erase(ConstantArray *C);
494491 void erase(ConstantVector *V);
495
496 // RAUW helpers
497
498 Constant *replaceUsesOfWithOnConstant(ConstantArray *CA, Value *From,
499 Value *To, Use *U);
500492 };
501493
502494 }
188188 Elts.push_back(TorList[i].first);
189189 ArrayElts.push_back(ConstantStruct::get(Elts));
190190 }
191 return Context.getConstantArray(Context.getArrayType(ArrayElts[0]->getType(),
191 return ConstantArray::get(Context.getArrayType(ArrayElts[0]->getType(),
192192 ArrayElts.size()),
193193 ArrayElts);
194194 }
708708 // Don't forward functions which are external in the test module too.
709709 if (TestFn && !TestFn->isDeclaration()) {
710710 // 1. Add a string constant with its name to the global file
711 Constant *InitArray = Context.getConstantArray(F->getName());
711 Constant *InitArray = ConstantArray::get(F->getName());
712712 GlobalVariable *funcName =
713713 new GlobalVariable(*Safe, InitArray->getType(), true /*isConstant*/,
714714 GlobalValue::InternalLinkage, InitArray,