llvm.org GIT mirror llvm / 28252b6
fix PR8932, a case where arg promotion could infinitely promote. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@123574 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 8 years ago
2 changed file(s) with 75 addition(s) and 27 deletion(s). Raw diff Collapse all Expand all
134134 if (PointerArgs.empty()) return 0;
135135
136136 // Second check: make sure that all callers are direct callers. We can't
137 // transform functions that have indirect callers.
138 if (F->hasAddressTaken())
139 return 0;
140
137 // transform functions that have indirect callers. Also see if the function
138 // is self-recursive.
139 bool isSelfRecursive = false;
140 for (Value::use_iterator UI = F->use_begin(), E = F->use_end();
141 UI != E; ++UI) {
142 CallSite CS(*UI);
143 // Must be a direct call.
144 if (CS.getInstruction() == 0 || !CS.isCallee(UI)) return 0;
145
146 if (CS.getInstruction()->getParent()->getParent() == F)
147 isSelfRecursive = true;
148 }
149
141150 // Check to see which arguments are promotable. If an argument is promotable,
142151 // add it to ArgsToPromote.
143152 SmallPtrSet ArgsToPromote;
144153 SmallPtrSet ByValArgsToTransform;
145154 for (unsigned i = 0; i != PointerArgs.size(); ++i) {
146155 bool isByVal = F->paramHasAttr(PointerArgs[i].second+1, Attribute::ByVal);
156 Argument *PtrArg = PointerArgs[i].first;
157 const Type *AgTy = cast(PtrArg->getType())->getElementType();
147158
148159 // If this is a byval argument, and if the aggregate type is small, just
149160 // pass the elements, which is always safe.
150 Argument *PtrArg = PointerArgs[i].first;
151161 if (isByVal) {
152 const Type *AgTy = cast(PtrArg->getType())->getElementType();
153162 if (const StructType *STy = dyn_cast(AgTy)) {
154163 if (maxElements > 0 && STy->getNumElements() > maxElements) {
155164 DEBUG(dbgs() << "argpromotion disable promoting argument '"
156165 << PtrArg->getName() << "' because it would require adding more"
157166 << " than " << maxElements << " arguments to the function.\n");
158 } else {
159 // If all the elements are single-value types, we can promote it.
160 bool AllSimple = true;
161 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
162 if (!STy->getElementType(i)->isSingleValueType()) {
163 AllSimple = false;
164 break;
165 }
166
167 // Safe to transform, don't even bother trying to "promote" it.
168 // Passing the elements as a scalar will allow scalarrepl to hack on
169 // the new alloca we introduce.
170 if (AllSimple) {
171 ByValArgsToTransform.insert(PtrArg);
172 continue;
167 continue;
168 }
169
170 // If all the elements are single-value types, we can promote it.
171 bool AllSimple = true;
172 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
173 if (!STy->getElementType(i)->isSingleValueType()) {
174 AllSimple = false;
175 break;
173176 }
174177 }
175 }
176 }
177
178
179 // Safe to transform, don't even bother trying to "promote" it.
180 // Passing the elements as a scalar will allow scalarrepl to hack on
181 // the new alloca we introduce.
182 if (AllSimple) {
183 ByValArgsToTransform.insert(PtrArg);
184 continue;
185 }
186 }
187 }
188
189 // If the argument is a recursive type and we're in a recursive
190 // function, we could end up infinitely peeling the function argument.
191 if (isSelfRecursive) {
192 if (const StructType *STy = dyn_cast(AgTy)) {
193 bool RecursiveType = false;
194 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
195 if (STy->getElementType(i) == PtrArg->getType()) {
196 RecursiveType = true;
197 break;
198 }
199 }
200 if (RecursiveType)
201 continue;
202 }
203 }
204
178205 // Otherwise, see if we can promote the pointer to its value.
179206 if (isSafeToPromoteArgument(PtrArg, isByVal))
180207 ArgsToPromote.insert(PtrArg);
187214 return DoPromotion(F, ArgsToPromote, ByValArgsToTransform);
188215 }
189216
190 /// AllCalleesPassInValidPointerForArgument - Return true if we can prove that
217 /// AllCallersPassInValidPointerForArgument - Return true if we can prove that
191218 /// all callees pass in a valid pointer for the specified function argument.
192 static bool AllCalleesPassInValidPointerForArgument(Argument *Arg) {
219 static bool AllCallersPassInValidPointerForArgument(Argument *Arg) {
193220 Function *Callee = Arg->getParent();
194221
195222 unsigned ArgNo = std::distance(Callee->arg_begin(),
309336 GEPIndicesSet ToPromote;
310337
311338 // If the pointer is always valid, any load with first index 0 is valid.
312 if (isByVal || AllCalleesPassInValidPointerForArgument(Arg))
339 if (isByVal || AllCallersPassInValidPointerForArgument(Arg))
313340 SafeToUnconditionallyLoad.insert(IndicesVector(1, 0));
314341
315342 // First, iterate the entry block and mark loads of (geps of) arguments as
3535 ret i1 undef
3636 }
3737
38
39 ; PR8932 - infinite promotion.
40 %0 = type { %0* }
41
42 define i32 @test2(i32 %a) {
43 init:
44 %0 = alloca %0
45 %1 = alloca %0
46 %2 = call i32 @"clay_assign(Chain, Chain)"(%0* %0, %0* %1)
47 ret i32 0
48 }
49
50 define internal i32 @"clay_assign(Chain, Chain)"(%0* %c, %0* %d) {
51 init:
52 %0 = getelementptr %0* %d, i32 0, i32 0
53 %1 = load %0** %0
54 %2 = getelementptr %0* %c, i32 0, i32 0
55 %3 = load %0** %2
56 %4 = call i32 @"clay_assign(Chain, Chain)"(%0* %3, %0* %1)
57 ret i32 0
58 }