llvm.org GIT mirror llvm / ae316aa
Push isDereferenceableAndAlignedPointer down into isSafeToLoadUnconditionally Reviewed By: reames Differential Revision: http://reviews.llvm.org/D16226 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@258010 91177308-0d34-0410-b5e6-96231b3b80d8 Artur Pilipenko 3 years ago
6 changed file(s) with 78 addition(s) and 17 deletion(s). Raw diff Collapse all Expand all
6969 if (Align == 0)
7070 Align = DL.getABITypeAlignment(V->getType()->getPointerElementType());
7171 assert(isPowerOf2_32(Align));
72
73 if (isDereferenceableAndAlignedPointer(V, Align, DL))
74 return true;
7275
7376 int64_t ByteOffset = 0;
7477 Value *Base = V;
11911191 // If this pointer is always safe to load, or if we can prove that there
11921192 // is already a load in the block, then we can move the load to the pred
11931193 // block.
1194 if (isDereferenceablePointer(InVal, DL) ||
1195 isSafeToLoadUnconditionally(InVal, MaxAlign, TI))
1194 if (isSafeToLoadUnconditionally(InVal, MaxAlign, TI))
11961195 continue;
11971196
11981197 return false;
12611260 Value *TValue = SI.getTrueValue();
12621261 Value *FValue = SI.getFalseValue();
12631262 const DataLayout &DL = SI.getModule()->getDataLayout();
1264 bool TDerefable = isDereferenceablePointer(TValue, DL);
1265 bool FDerefable = isDereferenceablePointer(FValue, DL);
12661263
12671264 for (User *U : SI.users()) {
12681265 LoadInst *LI = dyn_cast(U);
12721269 // Both operands to the select need to be dereferencable, either
12731270 // absolutely (e.g. allocas) or at this point because we can see other
12741271 // accesses to it.
1275 if (!TDerefable &&
1276 !isSafeToLoadUnconditionally(TValue, LI->getAlignment(), LI))
1272 if (!isSafeToLoadUnconditionally(TValue, LI->getAlignment(), LI))
12771273 return false;
1278 if (!FDerefable &&
1279 !isSafeToLoadUnconditionally(FValue, LI->getAlignment(), LI))
1274 if (!isSafeToLoadUnconditionally(FValue, LI->getAlignment(), LI))
12801275 return false;
12811276 }
12821277
11401140 /// the select can be loaded unconditionally.
11411141 static bool isSafeSelectToSpeculate(SelectInst *SI) {
11421142 const DataLayout &DL = SI->getModule()->getDataLayout();
1143 bool TDerefable = isDereferenceablePointer(SI->getTrueValue(), DL);
1144 bool FDerefable = isDereferenceablePointer(SI->getFalseValue(), DL);
11451143
11461144 for (User *U : SI->users()) {
11471145 LoadInst *LI = dyn_cast(U);
11491147
11501148 // Both operands to the select need to be dereferencable, either absolutely
11511149 // (e.g. allocas) or at this point because we can see other accesses to it.
1152 if (!TDerefable &&
1153 !isSafeToLoadUnconditionally(SI->getTrueValue(), LI->getAlignment(),
1150 if (!isSafeToLoadUnconditionally(SI->getTrueValue(), LI->getAlignment(),
11541151 LI))
11551152 return false;
1156 if (!FDerefable &&
1157 !isSafeToLoadUnconditionally(SI->getFalseValue(), LI->getAlignment(),
1153 if (!isSafeToLoadUnconditionally(SI->getFalseValue(), LI->getAlignment(),
11581154 LI))
11591155 return false;
11601156 }
12281224
12291225 // If this pointer is always safe to load, or if we can prove that there is
12301226 // already a load in the block, then we can move the load to the pred block.
1231 if (isDereferenceablePointer(InVal, DL) ||
1232 isSafeToLoadUnconditionally(InVal, MaxAlign, Pred->getTerminator()))
1227 if (isSafeToLoadUnconditionally(InVal, MaxAlign, Pred->getTerminator()))
12331228 continue;
12341229
12351230 return false;
12951295 ret i32 %v
12961296 }
12971297
1298 define i32 @test78_deref(i1 %flag, i32* dereferenceable(4) %x, i32* dereferenceable(4) %y, i32* %z) {
1299 ; Test that we can speculate the loads around the select even when we can't
1300 ; fold the load completely away.
1301 ; CHECK-LABEL: @test78_deref(
1302 ; CHECK: %[[V1:.*]] = load i32, i32* %x
1303 ; CHECK-NEXT: %[[V2:.*]] = load i32, i32* %y
1304 ; CHECK-NEXT: %[[S:.*]] = select i1 %flag, i32 %[[V1]], i32 %[[V2]]
1305 ; CHECK-NEXT: ret i32 %[[S]]
1306 entry:
1307 %p = select i1 %flag, i32* %x, i32* %y
1308 %v = load i32, i32* %p
1309 ret i32 %v
1310 }
1311
12981312 define i32 @test78_neg(i1 %flag, i32* %x, i32* %y, i32* %z) {
12991313 ; The same as @test78 but we can't speculate the load because it can trap
13001314 ; if under-aligned.
13091323 store i32 42, i32* %z
13101324 %p = select i1 %flag, i32* %x, i32* %y
13111325 %v = load i32, i32* %p, align 16
1326 ret i32 %v
1327 }
1328
1329 define i32 @test78_deref_neg(i1 %flag, i32* dereferenceable(2) %x, i32* dereferenceable(4) %y, i32* %z) {
1330 ; The same as @test78_deref but we can't speculate the load because
1331 ; one of the arguments is not sufficiently dereferenceable.
1332 ; CHECK-LABEL: @test78_deref_neg(
1333 ; CHECK: %p = select i1 %flag, i32* %x, i32* %y
1334 ; CHECK-NEXT: %v = load i32, i32* %p
1335 ; CHECK-NEXT: ret i32 %v
1336 entry:
1337 %p = select i1 %flag, i32* %x, i32* %y
1338 %v = load i32, i32* %p
13121339 ret i32 %v
13131340 }
13141341
None ; RUN: opt < %s -tailcallelim -S | grep call | count 3
0 ; RUN: opt < %s -tailcallelim -S | grep call | count 4
11 ; PR4323
22
33 ; Several cases where tail call elimination should not move the load above the
6161 %tmp10 = add i32 %tmp9, %tmp8 ; [#uses=1]
6262 ret i32 %tmp10
6363 }
64
65 ; This load can NOT be moved above the call because the a_arg is not
66 ; sufficiently dereferenceable.
67 define fastcc i32 @no_tailrecelim_4(i32* dereferenceable(2) %a_arg, i32 %a_len_arg, i32 %start_arg) readonly {
68 entry:
69 %tmp2 = icmp sge i32 %start_arg, %a_len_arg ; [#uses=1]
70 br i1 %tmp2, label %if, label %else
71
72 if: ; preds = %entry
73 ret i32 0
74
75 else: ; preds = %entry
76 %tmp7 = add i32 %start_arg, 1 ; [#uses=1]
77 %tmp8 = call fastcc i32 @no_tailrecelim_4(i32* %a_arg, i32 %a_len_arg, i32 %tmp7) ; [#uses=1]
78 %tmp9 = load i32, i32* %a_arg ; [#uses=1]
79 %tmp10 = add i32 %tmp9, %tmp8 ; [#uses=1]
80 ret i32 %tmp10
81 }
121121 %tmp10 = add i32 %second, %tmp8 ; [#uses=1]
122122 ret i32 %tmp10
123123 }
124
125 ; This load can be moved above the call because the function won't write to it
126 ; and the a_arg is dereferenceable.
127 define fastcc i32 @raise_load_5(i32* dereferenceable(4) %a_arg, i32 %a_len_arg, i32 %start_arg) readonly {
128 ; CHECK-LABEL: @raise_load_5(
129 ; CHECK-NOT: call
130 ; CHECK: load i32, i32*
131 ; CHECK-NOT: call
132 ; CHECK: }
133 entry:
134 %tmp2 = icmp sge i32 %start_arg, %a_len_arg ; [#uses=1]
135 br i1 %tmp2, label %if, label %else
136
137 if: ; preds = %entry
138 ret i32 0
139
140 else: ; preds = %entry
141 %tmp7 = add i32 %start_arg, 1 ; [#uses=1]
142 %tmp8 = call fastcc i32 @raise_load_5(i32* %a_arg, i32 %a_len_arg, i32 %tmp7) ; [#uses=1]
143 %tmp9 = load i32, i32* %a_arg ; [#uses=1]
144 %tmp10 = add i32 %tmp9, %tmp8 ; [#uses=1]
145 ret i32 %tmp10
146 }