llvm.org GIT mirror llvm / c2afe89
Reapply r52397 (make IPConstProp promote returned arguments), but fixed this time. Sorry for the trouble! This time, also add a testcase, which I should have done in the first place... git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@52455 91177308-0d34-0410-b5e6-96231b3b80d8 Matthijs Kooijman 11 years ago
2 changed file(s) with 64 addition(s) and 3 deletion(s). Raw diff Collapse all Expand all
144144 // all callers that use those return values with the constant value. This will
145145 // leave in the actual return values and instructions, but deadargelim will
146146 // clean that up.
147 //
148 // Additionally if a function always returns one of its arguments directly,
149 // callers will be updated to use the value they pass in directly instead of
150 // using the return value.
147151 bool IPCP::PropagateConstantReturn(Function &F) {
148152 if (F.getReturnType() == Type::VoidTy)
149153 return false; // No return value.
187191 if (isa(V))
188192 continue;
189193
190 // Try to see if all the rets return the same constant.
191 if (isa(V)) {
194 // Try to see if all the rets return the same constant or argument.
195 if (isa(V) || isa(V)) {
192196 if (isa(RV)) {
193197 // No value found yet? Try the current one.
194198 RetVals[i] = V;
226230 MadeChange = true;
227231
228232 if (STy == 0) {
229 Call->replaceAllUsesWith(RetVals[0]);
233 Value* New = RetVals[0];
234 if (Argument *A = dyn_cast(New))
235 // Was an argument returned? Then find the corresponding argument in
236 // the call instruction and use that. Add 1 to the argument number
237 // to skip the first argument (the function itself).
238 New = Call->getOperand(A->getArgNo() + 1);
239 Call->replaceAllUsesWith(New);
230240 continue;
231241 }
232242
254264 if (index != -1) {
255265 Value *New = RetVals[index];
256266 if (New) {
267 if (Argument *A = dyn_cast(New))
268 // Was an argument returned? Then find the corresponding argument in
269 // the call instruction and use that. Add 1 to the argument number
270 // to skip the first argument (the function itself).
271 New = Call->getOperand(A->getArgNo() + 1);
257272 Ins->replaceAllUsesWith(New);
258273 Ins->eraseFromParent();
259274 }
0 ; RUN: llvm-as < %s | opt -ipconstprop | llvm-dis > %t
1 ; RUN: cat %t | grep {store i32 %Z, i32\\* %Q}
2 ; RUN: cat %t | grep {add i32 1, 3}
3
4 ;; This function returns its second argument on all return statements
5 define internal i32* @incdec(i1 %C, i32* %V) {
6 %X = load i32* %V
7 br i1 %C, label %T, label %F
8
9 T: ; preds = %0
10 %X1 = add i32 %X, 1
11 store i32 %X1, i32* %V
12 ret i32* %V
13
14 F: ; preds = %0
15 %X2 = sub i32 %X, 1
16 store i32 %X2, i32* %V
17 ret i32* %V
18 }
19
20 ;; This function returns its first argument as a part of a multiple return
21 ;; value
22 define internal { i32, i32 } @foo(i32 %A, i32 %B) {
23 %X = add i32 %A, %B
24 %Y = insertvalue { i32, i32 } undef, i32 %A, 0
25 %Z = insertvalue { i32, i32 } %Y, i32 %X, 1
26 ret { i32, i32 } %Z
27 }
28
29 define void @caller(i1 %C) {
30 %Q = alloca i32
31 ;; Call incdec to see if %W is properly replaced by %Q
32 %W = call i32* @incdec(i1 %C, i32* %Q ) ; [#uses=1]
33 ;; Call @foo twice, to prevent the arguments from propagating into the
34 ;; function (so we can check the returned argument is properly
35 ;; propagated per-caller).
36 %S1 = call { i32, i32 } @foo(i32 1, i32 2);
37 %X1 = extractvalue { i32, i32 } %S1, 0
38 %S2 = call { i32, i32 } @foo(i32 3, i32 4);
39 %X2 = extractvalue { i32, i32 } %S2, 0
40 ;; Do some stuff with the returned values which we can grep for
41 %Z = add i32 %X1, %X2
42 store i32 %Z, i32* %W
43 ret void
44 }
45